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

108 Views Asked by At

While learning Python, I decided to mess around with Python's csv module using a Kaggle data set containing the most popular emojis.

Here's a small sample:

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

I wanted to use a one-liner from tabulate to make a table with the data. Unfortunately, I kept finding unwanted spaces after any character that used multiple UTF-8 values.

Here is my code:

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"))

Here is the result: https://i.stack.imgur.com/Z3z82.png

1

There are 1 best solutions below

2
nisakova On

what about strip them ?

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

update: tested with prettytable package enter image description here