我正在從 dict 形成一個 BeautifulTable 表,其鍵是數字字串集(例如“21608”、“32099”、“02978”)。dict 的鍵將成為我表的第一列:
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 () 方法中?我不太明白為什么它會在輸出期間以任何方式修改字串型別。任何想法,我怎么能避免它?
uj5u.com熱心網友回復:
它正在轉換為 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 |
----- --------
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/436147.html
標籤:Python 细绳 输出 代表 python-beautifultable
上一篇:如何使用模式從字串中獲取特定資料
下一篇:在框架內列印字串
