如何仅读取CSV文件每一行的第一列 [英] How to read just the first column of each row of a CSV file
本文介绍了如何仅读取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]
这篇关于如何仅读取CSV文件每一行的第一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文