gpt4 book ai didi

python - 在python单个表格单元格中添加多行

转载 作者:行者123 更新时间:2023-12-04 17:49:02 24 4
gpt4 key购买 nike

我不确定如何绘制如下所示的表格,我尝试使用漂亮的表格但无法在一个单元格中放置多行。

注意:行数应该基于字符串的数量,所以我想每行放一些 n 个字符串。

有人可以帮忙吗?

+---- +-------------------+-------------------------------------------------------+
| Id | Name | Comment |
+-----+-------------------+-------------------------------------------------------+
| 1 | Alvina Skiles | Dolor qui rerum est sed. Sed ipsa repudiandae et |
| | | Non explicabo voluptas impedit rerum dignissimos. |
| | | Minima voluptatibus sint voluptates similique.' |
+-----+-------------------+-------------------------------------------------------+
| 2 | Chasity Lakin | Nesciunt ea voluptatem rerum eos rerum ut soluta |
| | | Animi totam rerum fugiat consectetur odio et |
| | | repellendus |
+-----+-------------------+-------------------------------------------------------+
| 3 | Miss Brennan Kiehn| Nulla placeat saepe voluptatem molestias dolores ex |
| | | Reiciendis nostrum adipisci qui enim explicabo. |
+-----+-------------------+-------------------------------------------------------+

这是我构建表的数据结构:
[
{
"id": "1",
"name": "Alvina Skiles",
"comment": 'Dolor qui rerum est sed. Sed ipsa repudiandae et. Non explicabo voluptas impedit rerum dignissimos. Minima voluptatibus
},
{
"id": "2",
"name" : 'Chasity Lakin',
"comment": 'Nesciunt ea voluptatem rerum eos rerum ut soluta. Animi totam rerum fugiat consectetur odio et repellendus.',
},
{
"id": "3",
"name" : 'Miss Brennan Kiehn',
"comment": 'Nulla placeat saepe voluptatem molestias dolores ex. Reiciendis nostrum adipisci qui enim explicabo.
},
]

最佳答案

看起来prettytable 可以很好地处理换行符,因此快速格式化功能可能就可以解决问题。这是我的示例:

import prettytable
from prettytable import ALL as ALL
items_table = prettytable.PrettyTable(hrules=ALL)
items_table.field_names = ["id", "name", "comment"]

items = [
{
"id": "1",
"name": "Alvina Skiles",
"comment": 'Dolor qui rerum est sed. Sed ipsa repudiandae et. Non explicabo voluptas impedit rerum dignissimos. Minima voluptatibus'
},
{
"id": "2",
"name" : 'Chasity Lakin',
"comment": 'Nesciunt ea voluptatem rerum eos rerum ut soluta. Animi totam rerum fugiat consectetur odio et repellendus.'
},
{
"id": "3",
"name" : 'Miss Brennan Kiehn',
"comment": 'Nulla placeat saepe voluptatem molestias dolores ex. Reiciendis nostrum adipisci qui enim explicabo.'
},
]

def format_comment(comment, max_line_length):
#accumulated line length
ACC_length = 0
words = comment.split(" ")
formatted_comment = ""
for word in words:
#if ACC_length + len(word) and a space is <= max_line_length
if ACC_length + (len(word) + 1) <= max_line_length:
#append the word and a space
formatted_comment = formatted_comment + word + " "
#length = length + length of word + length of space
ACC_length = ACC_length + len(word) + 1
else:
#append a line break, then the word and a space
formatted_comment = formatted_comment + "\n" + word + " "
#reset counter of length to the length of a word and a space
ACC_length = len(word) + 1
return formatted_comment

for item in items:
item["comment"] = format_comment(item["comment"], 42)
items_table.add_row([item["id"], item["name"], item["comment"]])

print(items_table)

这是输出:
+----+--------------------+--------------------------------------------+
| id | name | comment |
+----+--------------------+--------------------------------------------+
| 1 | Alvina Skiles | Dolor qui rerum est sed. Sed ipsa |
| | | repudiandae et. Non explicabo voluptas |
| | | impedit rerum dignissimos. Minima |
| | | voluptatibus |
+----+--------------------+--------------------------------------------+
| 2 | Chasity Lakin | Nesciunt ea voluptatem rerum eos rerum ut |
| | | soluta. Animi totam rerum fugiat |
| | | consectetur odio et repellendus. |
+----+--------------------+--------------------------------------------+
| 3 | Miss Brennan Kiehn | Nulla placeat saepe voluptatem molestias |
| | | dolores ex. Reiciendis nostrum adipisci |
| | | qui enim explicabo. |
+----+--------------------+--------------------------------------------+

关于python - 在python单个表格单元格中添加多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22560768/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com