Python的BeautifulTable修改输出
我正在从 dict 形成一个 BeautifulTable 表,其键是数字字符串集(例如“21608”、“32099”、“02978”)。字典的键将成为我的表的第一列:
for (key, value) in stations.items():
table.rows.append([key] + value) # 'value' is a list, so here I just concatenate [key] and [value1, ..., valueN] to form new list [key, value1, ..., valueN] to add to the table
当我尝试使用 print(table) 命令打印表(到 stdout 或 txt 文件)时,就会出现问题。
for (key, value) in stations.items():
table.rows.append([key] + value)
print(table)
问题是:第一列的所有以“0”开头的数字集(例如“02978”、“02186”)都被修改,以便第一个“0”被删除。
然后我尝试一一打印出行,将它们转换为列表将它们附加到表中:
for (key, value) in stations.items():
table.rows.append([key] + value)
print(list(table.rows[-1]))
此输出根据需要显示数据,不删除任何零:
为什么会出现这个结果?原因是在 BeautifulTable 表的 repr() 方法中?我不太明白为什么它会在输出过程中以任何方式修改字符串类型。有什么想法,我怎样才能避免它?
I'm forming a BeautifulTable table from dict, whose keys are string sets of digits (e.g. "21608", "32099", "02978"). The keys of the dict are to become the first column of my table:
for (key, value) in stations.items():
table.rows.append([key] + value) # 'value' is a list, so here I just concatenate [key] and [value1, ..., valueN] to form new list [key, value1, ..., valueN] to add to the table
The problem occurs when I try to print out (to stdout or a txt file) the table using print(table) command.
for (key, value) in stations.items():
table.rows.append([key] + value)
print(table)
And the problem is: all the first column's sets of digits, that are starting with "0" (e.g. "02978", "02186"), are modified so that the first "0" gets stripped.
Then I tried to print out rows one by one convering them to list just after I append them to table:
for (key, value) in stations.items():
table.rows.append([key] + value)
print(list(table.rows[-1]))
This output shows data as needed, with no zeroes stripped:
Why this result? The reason is in repr() method of BeautifulTable tables? I don't quite understand why whould it modify in any way the string types during the output. Any ideas, how could I avoid it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它正在转换为 int/float
您可以使用
detect_numerics=False
禁用此行为。It's being converted into an int/float
You can use
detect_numerics=False
to disable this behaviour.