【发布时间】:2021-07-01 21:18:41
【问题描述】:
我有一个文本文件,我想将其格式化为 pandas 数据框。它被读取为以下形式的字符串:print(text)=
product: 1
description: product 1 desc
rating: 7.8
review: product 1 review
product: 2
description: product 2 desc
rating: 4.5
review: product 2 review
product: 3
description: product 3 desc
rating: 8.5
review: product 3 review
我想我会通过使用 text.split('\n\n') 将它们分组到列表中来拆分它们。我会假设将每个迭代到一个 dict 中,然后加载到 pandas df 将是一个很好的路线,但我在这样做时遇到了麻烦。这是最好的路线吗,有人可以帮我把它变成熊猫 df 吗?
【问题讨论】:
标签: python pandas list loops dictionary