[英]Python tabulate - configure how to format cells containing lists
我正在使用 python-tabulate 打印包含列表的單元格的數據。 我可以自定義 python-tabulate 如何格式化單元格內容,以便以不同的方式格式化列表嗎?
我想避免手動預處理(將列表轉換為字符串)傳遞給制表的數據。
如果制表不允許這樣做,是否有替代庫可以定義更細粒度的格式選項?
示例代碼:
from tabulate import tabulate
# Minimal sample. My output comes from an API and contains much more data
table = [['Sun', [1, 2, 3]], ['Moon', [4, 5, 6]]]
print(tabulate(table, tablefmt='plain', headers=['Planet', 'Value']))
輸出:
Planet Value
Sun [1, 2, 3]
Moon [4, 5, 6]
我希望如何格式化輸出:
Planet Value
Sun 1,2,3
Moon 4,5,6
我建議你嘗試不同的圖書館。 我檢查了 DOC 並沒有找到任何相關的內容。
或者有它的功能(雖然你沒有要求它)
from tabulate import tabulate
def delist(lst):
return ",".join([str(item) for item in lst])
table = [['Sun', delist([1, 2, 3])], ['Moon', delist([4, 5, 6])]]
print(tabulate(table, tablefmt='plain', headers=['Planet', 'Value']))
>>>
Planet Value
Sun 1,2,3
Moon 4,5,6
最后一個選擇,也許有點無望......是采取制表類並將此功能集成到其中。 但它有點矯枉過正
由於我找不到如何相應地配置制表的方法,一個解決方案是猴子補丁制表內部 _format 方法。
原始方法(表 0.8.2):
def _format(val, valtype, floatfmt, missingval="", has_invisible=True):
"""Format a value accoding to its type.
Unicode is supported:
>>> hrow = ['\u0431\u0443\u043a\u0432\u0430', '\u0446\u0438\u0444\u0440\u0430'] ; \
tbl = [['\u0430\u0437', 2], ['\u0431\u0443\u043a\u0438', 4]] ; \
good_result = '\\u0431\\u0443\\u043a\\u0432\\u0430 \\u0446\\u0438\\u0444\\u0440\\u0430\\n------- -------\\n\\u0430\\u0437 2\\n\\u0431\\u0443\\u043a\\u0438 4' ; \
tabulate(tbl, headers=hrow) == good_result
True
"""
if val is None:
return missingval
if valtype in [int, _text_type]:
return "{0}".format(val)
elif valtype is _binary_type:
try:
return _text_type(val, "ascii")
except TypeError:
return _text_type(val)
elif valtype is float:
is_a_colored_number = has_invisible and isinstance(val, (_text_type, _binary_type))
if is_a_colored_number:
raw_val = _strip_invisible(val)
formatted_val = format(float(raw_val), floatfmt)
return val.replace(raw_val, formatted_val)
else:
return format(float(val), floatfmt)
else:
return "{0}".format(val)
我的修改版本:
# tabulate_extensions.py
from tabulate import _text_type, _binary_type, _strip_invisible
def _format_extended(val, valtype, floatfmt, missingval="", has_invisible=True):
"""Format a value accoding to its type.
Unicode is supported:
>>> hrow = ['\u0431\u0443\u043a\u0432\u0430', '\u0446\u0438\u0444\u0440\u0430'] ; \
tbl = [['\u0430\u0437', 2], ['\u0431\u0443\u043a\u0438', 4]] ; \
good_result = '\\u0431\\u0443\\u043a\\u0432\\u0430 \\u0446\\u0438\\u0444\\u0440\\u0430\\n------- -------\\n\\u0430\\u0437 2\\n\\u0431\\u0443\\u043a\\u0438 4' ; \
tabulate(tbl, headers=hrow) == good_result
True
"""
if val is None:
return missingval
if valtype in [int, _text_type]:
# Change list formatting [1,2,3] -> 1,2,3
if type(val) == list:
val = ','.join([str(x) for x in val])
return "{0}".format(val)
elif valtype is _binary_type:
try:
return _text_type(val, "ascii")
except TypeError:
return _text_type(val)
elif valtype is float:
is_a_colored_number = has_invisible and isinstance(val, (_text_type, _binary_type))
if is_a_colored_number:
raw_val = _strip_invisible(val)
formatted_val = format(float(raw_val), floatfmt)
return val.replace(raw_val, formatted_val)
else:
return format(float(val), floatfmt)
else:
return "{0}".format(val)
在我的代碼中,我將 tabulates 內部方法替換為我的,如下所示:
from mypkg.tabulate_extensions import _format_extended
tabulate._format = _format_extended
輸出現在如所願。 好消息是我現在可以以任何我想要的方式擴展其他單元格類型(如字典)的格式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.