如何仅读取CSV文件每一行的第一列
问题描述:
如何在Python中仅读取CSV文件每一行的第一列?
How to read just the first column of each row of a CSV file in Python?
我的数据是这样的:
1 abc
2 bcd
3 cde
,我只需要循环遍历第一列的值.
and I only need to loop trough the values of the first column.
此外,当我在calc中打开csv文件时,每一行中的数据都在同一单元格中,这正常吗?
Also, when I open the csv File in calc the data in each row is all in the same cell, is that normal?
答
import csv
with open(file) as f:
reader = csv.reader(f, delimiter="\t")
for i in reader:
print i[0]
OR
如有必要,将除臭剂更改为空格.
change the delimter to space if necessary.
reader = csv.reader(f, delimiter=" ")
没有CSV模块,
import csv
with open(file) as f:
for line in f:
print line.split()[0]