将一列csv解压缩到逗号分隔的列表python中

abn*_*abn 0 python csv split strip comma

我有一个CSV文件,我在下面读到:

with open ("ann.csv", "rb") as annotate:
    for col in annotate:
        ann = col.lower().split(",")
        print ann[0]
Run Code Online (Sandbox Code Playgroud)

我的CSV文件如下所示:

H1,H2,H3
da,ta,one
dat,a,two
Run Code Online (Sandbox Code Playgroud)

我的输出如下:

da
dat
Run Code Online (Sandbox Code Playgroud)

但我想要一个逗号分隔的输出,如(da,dat).我怎样才能做到这一点?如果你在downvoting之前给我一个想法,我真的很感激.

Tim*_*ker 5

首先,在Python中你有csv模块 - 使用它.

其次,您正在遍历行,因此使用col变量名称有点令人困惑.

第三,只需收集列表中的项目并使用.join()以下方式打印:

import csv
with open ("ann.csv", "rb") as csvfile:
    reader = csv.reader(csvfile)
    reader.next() # Skip the header row
    collected = []
    for row in reader:
        collected.append(row[0])
    print ",".join(collected)
Run Code Online (Sandbox Code Playgroud)