Python的BeautifulTable修改输出

发布于 2025-01-10 12:21:24 字数 1192 浏览 0 评论 0原文

我正在从 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.

enter image description hereThe output table

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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不疑不惑不回忆 2025-01-17 12:21:24

它正在转换为 int/float

当前的行为是,如果一个字符串可以解析为 float/int,它将被解析为 float/int

>>> import beautifultable
>>> table = beautifultable.BeautifulTable()
>>> table.append_row(['foo', '012345'])
>>> print(table)
+-----+-------+
| foo | 12345 |
+-----+-------+

您可以使用 detect_numerics=False 禁用此行为。

>>> table = beautifultable.BeautifulTable(detect_numerics=False)
>>> table.append_row(['foo', '012345'])
>>> print(table)
+-----+--------+
| foo | 012345 |
+-----+--------+

It's being converted into an int/float

The current behaviour is that, if a string can be parsed as a float/int, it will be parsed as a float/int

>>> import beautifultable
>>> table = beautifultable.BeautifulTable()
>>> table.append_row(['foo', '012345'])
>>> print(table)
+-----+-------+
| foo | 12345 |
+-----+-------+

You can use detect_numerics=False to disable this behaviour.

>>> table = beautifultable.BeautifulTable(detect_numerics=False)
>>> table.append_row(['foo', '012345'])
>>> print(table)
+-----+--------+
| foo | 012345 |
+-----+--------+
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文