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

dphi5xsq  于 2023-09-27  发布在  其他
关注(0)|答案(1)|浏览(87)

在学习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

我想使用tabulate中的一行程序来创建一个包含数据的表格。不幸的是,我总是在使用多个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

oxcyiej7

oxcyiej71#

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

相关问题