用 Python 从 CSV 文件中读取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26903304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading data from a CSV file in Python
提问by Rohit
I am reading data from a CSV file (xyz.CSV) which contains below data:
我正在从包含以下数据的 CSV 文件 (xyz.CSV) 中读取数据:
col1,col2,col3,col4
name1,empId1,241682-27638-USD-CIGGNT ,1
name2,empId2,241682-27638-USD-OCGGINT ,1
name3,empId3,241942-37190-USD-GGDIV ,2
name4,empId4,241942-37190-USD-CHYOF ,1
name5,empId5,241942-37190-USD-EQPL ,1
name6,empId6,241942-37190-USD-INT ,1
name7,empId7,242066-15343-USD-CYJOF ,3
name8,empId8,242066-15343-USD-CYJOF ,3
name9,empId9,242066-15343-USD-CYJOF ,3
name10,empId10,241942-37190-USD-GGDIV ,2
When I am iterating it with a loop I am able to print the data row wise and and only column1 data by the below code.
当我用循环迭代它时,我能够通过下面的代码逐行打印数据,并且仅打印 column1 数据。
file=open( path +"xyz.CSV", "r")
reader = csv.reader(file)
for line in reader:
t=line[0]
print t
By the above code I can only get the first column.
通过上面的代码,我只能得到第一列。
If I try to print line[1] or line[2] it gives me the below error.
如果我尝试打印 line[1] 或 line[2],它会给我以下错误。
file=open( path +"xyz.CSV", "r")
reader = csv.reader(file)
for line in reader:
t=line[1],[2]
print t
t=line[1],line[2]
IndexError: list index out of range
Please suggest for printing the data of column2 or column3.
请建议打印 column2 或 column3 的数据。
采纳答案by Alderven
Here is how I've got 2nd and 3rd columns:
这是我如何获得第 2 列和第 3 列:
import csv
path = 'c:\temp\'
file=open( path +"xyz.CSV", "r")
reader = csv.reader(file)
for line in reader:
t=line[1],line[2]
print(t)
Here is the results:
结果如下:
('col2', 'col3')
('empId1', '241682-27638-USD-CIGGNT ')
('empId2', '241682-27638-USD-OCGGINT ')
('empId3', '241942-37190-USD-GGDIV ')
('empId4', '241942-37190-USD-CHYOF ')
('empId5', '241942-37190-USD-EQPL ')
('empId6', '241942-37190-USD-INT ')
('empId7', '242066-15343-USD-CYJOF ')
('empId8', '242066-15343-USD-CYJOF ')
('empId9', '242066-15343-USD-CYJOF ')
('empId10', '241942-37190-USD-GGDIV ')
回答by Aakash Jain
Hope it clears the issue
希望能解决问题
import csv
file=open( "xyz.CSV", "r")
reader = csv.reader(file)
for line in reader:
t=line[0]+","+line[1]
print (t)
回答by xecgr
Your first line only has one column, so the process fails and doesn't continue. To solve, just skip first row
您的第一行只有一列,因此该过程失败并且不会继续。要解决,只需跳过第一行
>>> with open( path, "r") as file:
... reader = csv.reader(file)
... for idx,line in enumerate(reader):
... if idx>0:
... t=line[1],line[2]
... print t
...
('empId1', '241682-27638-USD-CIGGNT ')
('empId2', '241682-27638-USD-OCGGINT ')
('empId3', '241942-37190-USD-GGDIV ')
('empId4', '241942-37190-USD-CHYOF ')
('empId5', '241942-37190-USD-EQPL ')
('empId6', '241942-37190-USD-INT ')
('empId7', '242066-15343-USD-CYJOF ')
('empId8', '242066-15343-USD-CYJOF ')
('empId9', '242066-15343-USD-CYJOF ')
('empId10', '241942-37190-USD-GGDIV ')
回答by Tanveer Alam
import csv
csv_file=open("xyz.csv", "r")
reader = csv.reader(csv_file)
for row in reader:
print(" ".join(row[:2]))
Output :-
col1 col2
name1 empId1
name2 empId2
name3 empId3
name4 empId4
name5 empId5
name6 empId6
name7 empId7
name8 empId8
name9 empId9
name10 empId10
Just put value in row as slice. Below is code for printing 2nd and 3rd coloumn.
只需将值作为切片放在行中。下面是打印第二和第三列的代码。
import csv
csv_file=open("xyz.csv", "r")
reader = csv.reader(csv_file)
for row in reader:
print(" ".join(row[1:3]))
output:
col2 col3
empId1 241682-27638-USD-CIGGNT
empId2 241682-27638-USD-OCGGINT
empId3 241942-37190-USD-GGDIV
empId4 241942-37190-USD-CHYOF
empId5 241942-37190-USD-EQPL
empId6 241942-37190-USD-INT
empId7 242066-15343-USD-CYJOF
empId8 242066-15343-USD-CYJOF
empId9 242066-15343-USD-CYJOF
empId10 241942-37190-USD-GGDIV
回答by mickey
To read and Write in a text file in Python, you can use the below syntax:
要在 Python 中读取和写入文本文件,您可以使用以下语法:
f = open('helloworld.txt','r')
message = f.read()
print(message)
f.close()
f = open('helloworld.txt','w')
f.write('hello world')
f.close()
To read the CSV file, folow the below code:
results = []enter code herewith open("C:/Users/Prateek/Desktop/TA Project/data1.csv") as inputfile:
for line in inputfile:
results.append(line.strip().split(','))
要读取 CSV 文件,请遵循以下代码: results = [] enter code herewith open("C:/Users/Prateek/Desktop/TA Project/data1.csv") as inputfile: for line in inputfile: results.append(line. strip().split(','))
回答by ABHISHEK SRIVASTAVA
There is a simple method you can check out more at: Python CSV Docs
有一个简单的方法,您可以在以下位置查看更多信息: Python CSV Docs
with open(filename, 'r') as csvfile:
spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
for row in spamreader:
data.append(row)
回答by Junaid
Although it's a pretty old question, just want to share my suggestion. Found it easier to read csv using pandas in a dataframe and access the data.
虽然这是一个很老的问题,但只是想分享我的建议。发现在数据框中使用 Pandas 读取 csv 并访问数据更容易。
import pandas
df = pandas.read_csv('<path/to/your/csv/file>')
print(df)
#OUTPUT
# col1 col2 col3 col4
#0 name1 empId1 241682-27638-USD-CIGGNT 1
#1 name2 empId2 241682-27638-USD-OCGGINT 1
#2 name3 empId3 241942-37190-USD-GGDIV 2
#3 name4 empId4 241942-37190-USD-CHYOF 1
#4 name5 empId5 241942-37190-USD-EQPL 1
#5 name6 empId6 241942-37190-USD-INT 1
#6 name7 empId7 242066-15343-USD-CYJOF 3
#7 name8 empId8 242066-15343-USD-CYJOF 3
#8 name9 empId9 242066-15343-USD-CYJOF 3
#9 name10 empId10 241942-37190-USD-GGDIV 2
#you can access any column using
df['col2']
#OUTPUT
#0 empId1
#1 empId2
#2 empId3
#3 empId4
#4 empId5
#5 empId6
#6 empId7
#7 empId8
#8 empId9
#9 empId10
#Name: col2, dtype: object
#Or print a specific value using
df['col2'][0]
Update: I was mainly using Pandas in my project so found it easier to just use it to read the csv as well. There are other dedicated libraries available to read CSV (creating your own CSV reader should also be few lines of code).
更新:我主要在我的项目中使用 Pandas,所以发现用它来读取 csv 也更容易。还有其他专用库可用于读取 CSV(创建自己的 CSV 阅读器也应该是几行代码)。

