在Python单元格中添加多行
我不太确定怎么画一个像下面这样的表格,我试过用prettytable,但无法在一个单元格里放多行内容。
注意:行数应该根据字符串的数量来决定,所以我想每行放一些字符串。
有人能帮帮我吗?
+---- +-------------------+-------------------------------------------------------+
| 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.
},
]
2 个回答
6
我今天也需要做同样的事情,查看了不同的解决方案后,我自己创建了这个:
from prettytable import PrettyTable
from textwrap import fill
header = ["Id", "Name", "Comment"]
table = PrettyTable(field_names=header, align='l')
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.'
},
]
for item in items:
table.add_row([item["id"], item["name"], fill(item["comment"], width=50)])
# you can change the width="number" to as many character you want per line.
print(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 自带的 textwrap 和 PrettyTable。
7
看起来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. |
+----+--------------------+--------------------------------------------+