【发布时间】:2018-07-17 18:06:43
【问题描述】:
我熟悉在 python 中读写 csv 文件的基本概念。但我坚持为这个问题制定一个逻辑。我认为 GROUP BY 可以解决我的问题,但是如何在 python 中做呢
Category Data
A Once upon a time.
A There was a king.
A who ruled a great and glorious nation.
B He loved each of them dearly.
B One day, when the young ladies were of age to be married.
B terrible, three-headed dragon laid.
C It is so difficult to deny
C the reality
我想为这样的输出制定逻辑,即 A 类的数据合并到一行,B 类和 C 类的数据也这样。
Category Data
A Once upon a time. There was a king. who ruled a great and glorious nation.
B He loved each of them dearly. One day, when the young ladies were of age to be married. terrible, three-headed dragon laid.
C It is so difficult to deny the reality
如果你们中的任何人可以帮助我解决这个逻辑,我会很感激他的努力。
【问题讨论】:
-
给定类别数据的初始列表,您可以使用 Python 的
groupby:docs.python.org/2/library/itertools.html#itertools.groupby,对category进行分组。使用它来连接数据,并创建一个新的 csv。
标签: python python-3.x csv