Python 将csv的一列提取到逗号分隔的列表中

Python 将csv的一列提取到逗号分隔的列表中,python,csv,split,comma,strip,Python,Csv,Split,Comma,Strip,我有一个CSV文件,如下所示: with open ("ann.csv", "rb") as annotate: for col in annotate: ann = col.lower().split(",") print ann[0] H1,H2,H3 da,ta,one dat,a,two 我的CSV文件如下所示: with open ("ann.csv", "rb") as annotate: for col in annotate:

我有一个CSV文件,如下所示:

with open ("ann.csv", "rb") as annotate:
    for col in annotate:
        ann = col.lower().split(",")
        print ann[0]
H1,H2,H3
da,ta,one
dat,a,two
我的CSV文件如下所示:

with open ("ann.csv", "rb") as annotate:
    for col in annotate:
        ann = col.lower().split(",")
        print ann[0]
H1,H2,H3
da,ta,one
dat,a,two
我的输出如下所示:

da
dat
with open ("ann.csv", "rb") as annotate:
    output = []
    next(annotate)    # next will advanced the file pointer to next line
    for col in annotate:
        output.append(col.lower().split(",")[0])
    print ",".join(output)

但是我想要一个逗号分隔的输出,比如(da,dat)。我该怎么做?如果你在投票前给我一个想法,我将不胜感激

不要当场打印,而是建立一个字符串,最后打印出来

s = ''
with open ("ann.csv", "rb") as annotate:
    for col in annotate:
        ann = col.lower().split(",")
        s += ann[0] + ','
s = s[:-1] # Remove last comma
print(s)
我还建议更改变量名
col
,它在行上循环,而不是在列上循环。

然后尝试以下操作:

result = ''
with open ("ann.csv", "rb") as annotate:
    for col in annotate:
        ann = col.lower().split(",")
        # add first element of every line to one string and separate them by comma
        result = result + ann[0] + ','

print result        

首先,在Python中,您有-使用它

其次,您要遍历行,因此使用
col
作为变量名有点混乱

第三,只需收集列表中的项目并使用
.join()
打印即可:

试试这个

>>> with open ("ann.csv", "rb") as annotate:
...     for col in annotate:
...         ann = col.lower().split(",")
...         print ann[0]+',',
... 
试着这样做:

da
dat
with open ("ann.csv", "rb") as annotate:
    output = []
    next(annotate)    # next will advanced the file pointer to next line
    for col in annotate:
        output.append(col.lower().split(",")[0])
    print ",".join(output)
使用可能更容易一些:

In [23]: import numpy as np
    ...: fn = 'a.csv'
    ...: m = np.loadtxt(fn, dtype=str, delimiter=',')
    ...: print m
[['H1' 'H2' 'H3']
 ['da' 'ta' 'one']
 ['dat' 'a' 'two']]

In [24]: m[:,0][1:]
Out[24]: 
array(['da', 'dat'], 
      dtype='|S3')

In [25]: print ','.join(m[:,0][1:])
da,dat

m[:,0]
获取矩阵的第一列
m
,并且
[1://code>跳过第一个元素
'H1'

您的意思是要将它们放在一行并用逗号分隔?@m170897017!你的脚本将实际输出
H1dadat
-你想不想跳过标题?@TimPietzcker我想。你的回答对我很有用。非常感谢。几分钟后我会接受的。Last+=应为a+。我改了。@Li aungYip:谢谢你加逗号,我忘了。没问题。和+1用于使用
csv
模块,而不是
split(',')
,这不允许在数据中使用逗号文本。