Python 3.x text在显示期间将字典列表中的字符串值换行

Python 3.x text在显示期间将字典列表中的字符串值换行,python-3.x,Python 3.x,我有一本如下所示的词典,但我无法对以下内容进行文本包装: dict= { 'ProductA' : ['2020-08-03 16:26:21', 'This painting was done by XNB.The artist seeks to portray the tragedies caused by event XYZ. The painting weighs 2kg.'], 'ProductB':['2020-08-03 16:26:21','This painting is d

我有一本如下所示的词典,但我无法对以下内容进行文本包装:

dict= { 'ProductA' : ['2020-08-03 16:26:21', 'This painting was done by XNB.The artist seeks to portray  the tragedies caused by event XYZ. The painting weighs 2kg.'], 'ProductB':['2020-08-03 16:26:21','This painting is done by ONN.This painting is done by ONN.Decades later, it was discovered in the black market of country XYZ.It was bought for 2 million dollars by ABC.']}
显示字典的预期结果:

  Product Name      Last Edited               Information
  Product A         2020-08-03 16:26:21       This painting was done by XNB.The artist 
                                              seeks to portray  the tragedies caused by
                                              event XYZ. The painting weighs 2kg.

  Product B         2020-08-03 16:26:21       This painting is done by ONN.This painting
                                              is done by ONN.Decades later, it was 
                                              discovered in the black market of country
                                              XYZ.It was bought for 2 million dollars 
                                              by ABC.
以下是我试图输出的显示,如上所述:

from textwrap import wrap

    print("{:<25} {:<30} {:<10}".format('Product Name','Last Edited','Information'))
for product_name, data in dict.items():
    last_edit, info = data
    wrapped_info=wrap(info, 45)
    
    print("{:<25} {:<30} {:<10}".format(product_name, last_edit, wrapped_info))
从文本包装导入包装

打印(“{:代码中的问题是
wrap()
返回字符串列表。您需要使用
join()
wrap()
的结果转换为字符串

试试这个:

from textwrap import wrap

d = { 'ProductA' : ['2020-08-03 16:26:21', 'This painting was done by XNB.The artist seeks to portray  the tragedies caused by event XYZ. The painting weighs 2kg.'], 'ProductB':['2020-08-03 16:26:21','This painting is done by ONN.This painting is done by ONN.Decades later, it was discovered in the black market of country XYZ.It was bought for 2 million dollars by ABC.']}

print("{:<25} {:<30} {:<10}".format('Product Name','Last Edited','Information'))
for product, data in d.items():
    date, info = data
    wrapped_info = f'\n{" " * (25 + 30 + 2)}'.join(wrap(info, 45)) 
    print("{:<25} {:<30} {:<10}\n".format(product, date, wrapped_info))

代码中的问题是
wrap()
返回字符串列表。您需要使用
join()
wrap()
的结果转换为字符串

试试这个:

from textwrap import wrap

d = { 'ProductA' : ['2020-08-03 16:26:21', 'This painting was done by XNB.The artist seeks to portray  the tragedies caused by event XYZ. The painting weighs 2kg.'], 'ProductB':['2020-08-03 16:26:21','This painting is done by ONN.This painting is done by ONN.Decades later, it was discovered in the black market of country XYZ.It was bought for 2 million dollars by ABC.']}

print("{:<25} {:<30} {:<10}".format('Product Name','Last Edited','Information'))
for product, data in d.items():
    date, info = data
    wrapped_info = f'\n{" " * (25 + 30 + 2)}'.join(wrap(info, 45)) 
    print("{:<25} {:<30} {:<10}\n".format(product, date, wrapped_info))

plant\u name,revision\u time
??请解决您的问题!你好,goodvibration,谢谢您指出拼写错误。我已经更正了。
plant\u name,revision\u time
?请解决您的问题!你好,goodvibration,谢谢您指出拼写错误。我已经更正了。你好,致命一击,请问是否有办法确保显示正确如果产品名称超过包装限制,ay格式不会迷失方向?您好,我想问一下,如果产品名称超过包装限制,是否有办法确保显示格式不会迷失方向?