在python单表单元格中添加多行
Posted
技术标签:
【中文标题】在python单表单元格中添加多行【英文标题】:Add multiple line in python single table cell 【发布时间】:2014-04-28 22:32:05 【问题描述】:我不知道如何绘制如下表格,我尝试使用漂亮的表格但无法在一个单元格中放置多行。
注意:行数应该根据字符串的个数,所以我想每行放一些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.
,
]
【问题讨论】:
这个标签后面的数据是如何表示的? 它是一个带有键 'id'、'name'、'likes' 的字典,当行长时,我想根据行中字符串的数量将其拆分为多行。 请更新问题,对要显示为表格的数据进行完整描述。生成示例表的数据示例将非常好。 数据示例与示例表不匹配。将评论分成几行的标准是什么? 我想根据行上的字符串数进行拆分,就像我想拆分的 10 个字符串一样。 【参考方案1】:我今天只需要同样的东西,并查看我创建的不同解决方案:
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 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. |
+----+--------------------+---------------------------------------------------+
就是这样,只需使用 textwrap(Python 内置)和 PrettyTable
【讨论】:
使用 prettyTable.add_row([item[row], width = 50]) 我得到一个语法错误。 你好@aJazz,实际上参数“width=50”不是'add_row'方法,而是一个textwrap.fill参数。看一下代码,特别注意括号。我确实在我身边再次运行了漏洞代码,它仍然运行良好。【参考方案2】:看起来 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单表单元格中添加多行的主要内容,如果未能解决你的问题,请参考以下文章