制表在特定长度的格网单元格的末尾插入一个空格

Tabulate inserts a space at the end of grid cells of a certain length

提问人:Baba20 提问时间:9/27/2023 最后编辑:Baba20 更新时间:9/28/2023 访问量:46

问:

在学习 Python 时,我决定使用包含最流行表情符号的 Kaggle 数据集来弄乱 Python 的 csv 模块。

下面是一个小示例:

Hex,Rank,Emoji,Year,Category,Subcategory,Name
\x{1F602},1,😂,2010,Smileys & Emotion,face-smiling,face with tears of joy
\x{2764 FE0F},2,❤️,2010,Smileys & Emotion,emotion,red heart
\x{1F923},3,🤣,2016,Smileys & Emotion,face-smiling,rolling on the floor laughing
\x{1F44D},4,👍,2010,People & Body,hand-fingers-closed,thumbs up
\x{1F62D},5,😭,2010,Smileys & Emotion,face-concerned,loudly crying face
\x{1F64F},6,🙏,2010,People & Body,hands,folded hands
\x{1F618},7,😘,2010,Smileys & Emotion,face-affection,face blowing a kiss
\x{1F970},8,🥰,2018,Smileys & Emotion,face-affection,smiling face with hearts
\x{1F60D},9,😍,2010,Smileys & Emotion,face-affection,smiling face with heart-eyes
\x{1F60A},10,😊,2010,Smileys & Emotion,face-smiling,smiling face with smiling eyes

我想使用表格中的单行来制作一个包含数据的表格。不幸的是,我一直在使用多个 UTF-8 值的任何字符后面找到不需要的空格。

这是我的代码:

import csv
from tabulate import tabulate as tb

top_n = 20

with open("emojis/emojis.csv", "r") as f:
    reader = [i for i in csv.reader(f)]
    print(tb([[j.replace("\\x", "") for j in i] for i in reader[:top_n + 1]], headers="firstrow", tablefmt="grid"))

结果如下:https://i.stack.imgur.com/Z3z82.png

python csv utf-8 表格

评论


答:

0赞 nisakova 9/27/2023 #1

剥去它们呢?

print(tb([[j.replace(“\x”, “”).strip() for j in i] for i in reader[:top_n + 1]], headers=“firstrow”, tablefmt=“grid”))

更新:使用Prettytable软件包进行测试enter image description here

评论

0赞 Baba20 9/27/2023
我对每个数组元素使用了“strip()”函数。这似乎是制表本身的问题。我打印了正常的表格数据,发现没有向任何列表项添加额外的空格。
0赞 nisakova 9/28/2023
嘿,我认为它不是表格,用不同的软件包测试,那一行仍然没有对齐