【发布时间】:2021-01-17 08:53:45
【问题描述】:
我有一个如下所示的 csv(注意:Name 列中的值不受限制,即不仅是 ABC 和 DEF):
Name, Type, Text
ABC, Type A, how
ABC, Type A, are
ABC, Type A, you
ABC, Type B, Your
ABC, Type B, Name?
DEF, Type A, I
DEF, Type A, am
DEF, Type A, good
DEF, Type B, I'm
DEF, Type B, Terminator
... and more
我想创建另一个 csv 文件,如下所示(即,基于每个 Type 列的 Text 列对每个 Name 列进行分组):
Name, Type, Text
ABC, Type A, how are you
ABC, Type B, Your Name?
DEF, Type A, I am good
DEF, Type B, I'm Terminator
..till the end
我正在尝试编写一个 python 脚本。我的尝试如下:
TypeList = ['Type A','Type B']
with open("../doc1.csv", encoding='utf-8', newline='', mode="r") as myfile:
g = csv.reader(myfile)
with open("../doc2.csv", encoding='utf-8', newline='', mode="w") as myfile:
h = csv.writer(myfile)
h.writerow(["Name","Text"])
for row in g:
if TypeList[0] in row[1]:
Concatenatedtext[0]= Concatenatedtext[0] + ' ' + row[1]
有人可以帮我解决这个问题吗?
【问题讨论】:
标签: python csv concatenation string-concatenation text-manipulation