python如何读取csv(python如何读取csv文件的数据)

网友投稿 3327 2022-06-10


csv是Comma-Separated Values的缩写,是用文本文件形式储存的表格数据,那么python如何读取csv呢?一起来了解下吧:

python如何读取csv

普通方法读取:

1 with open("fileName.csv") as file:

2 for line in file:

3 print line

用CSV标准库读取:

1 import csv

2 csv_reader = csv.reader(open("fileName.csv"))

3 for row in csv_reader:

4 print row

用pandas读取:

1 import pandas as pd

2 data = pd.read_csv("fileName.csv")

3 print data

4

5 data = pd.read_table("fileName.csv",sep=",")

6 print data

Python怎么读取csv的某行

csv是Comma-Separated Values的缩写,是用文本文件形式储存的表格数据

就可以存储为csv文件,文件内容是:

No.,Name,Age,Score

1,Apple,12,98

2,Ben,13,97

3,Celia,14,96

4,Dave,15,95

假设上述csv文件保存为"A.csv",如何用Python像操作Excel一样提取其中的一行,也就是一条记录,利用Python自带的csv模块,有两种方法可以实现:

第一种方法使用reader函数,接收一个可迭代的对象(比如csv文件),能返回一个生成器,就可以从其中解析出csv的内容:比如下面的代码可以读取csv的全部内容,以行为单位:import csv

with open('A.csv','rb') as csvfile:

reader = csv.reader(csvfile)

rows = [row for row in reader]

print rows得到:[['No.', 'Name', 'Age', 'Score'],

['1', 'Apple', '12', '98'],

['2', 'Ben', '13', '97'],

['3', 'Celia', '14', '96'],

['4', 'Dave', '15', '95']]

要提取其中第二行,可以用下面的代码:

import csv

with open('A.csv','rb') as csvfile:

reader = csv.reader(csvfile)

for i,rows in enumerate(reader):

if i == 2:

row = rows

print row 得到:['2', 'Ben', '13', '97']这种方法是通用的方法,要事先知道行号,比如Ben的记录在第2行,而不能根据'Ben'这个名字查询。这时可以采用第二种方法:

第二种方法是使用DictReader,和reader函数类似,接收一个可迭代的对象,能返回一个生成器,但是返回的每一个单元格都放在一个字典的值内,而这个字典的键则是这个单元格的标题(即列头)。用下面的代码可以看到DictReader的结构:

import csv

with open('A.csv','rb') as csvfile:

reader = csv.DictReader(csvfile)

rows = [row for row in reader]

print rows得到:

[{'Age': '12', 'No.': '1', 'Score': '98', 'Name': 'Apple'},

{'Age': '13', 'No.': '2', 'Score': '97', 'Name': 'Ben'},

{'Age': '14', 'No.': '3', 'Score': '96', 'Name': 'Celia'},

{'Age': '15', 'No.': '4', 'Score': '95', 'Name': 'Dave'}]

如果我们想用DictReader读取csv的某一列,就可以用列的标题查询:

import csv

with open('A.csv','rb') as csvfile:

reader = csv.DictReader(csvfile)

for row in reader:

if row['Name']=='Ben':

print row就得到:

{'Age': '13', 'No.': '2', 'Score': '97', 'Name': 'Ben'}可见,DictReader很适合读取csv的的行(记录)。

Python读取csv的方法

1.以行为单位存储csv内容:

import csv

with open('A.csv','rb') as csvfile:

reader = csv.reader(csvfile)

rows= [row for row in reader]

print rows

得到

[['No.', 'Name', 'Age', 'Score'],

['1', 'Apple', '12', '98'],

['2', 'Ben', '13', '97'],

['3', 'Celia', '14', '96'],

['4', 'Dave', '15', '95']]

2.读取csv的某一列 (用列的序号):

import csv

with open('A.csv','rb') as csvfile:

reader = csv.reader(csvfile)

column = [row[2] for row in reader]

print column

得到

['Age', '12', '13', '14', '15']

3.读取csv的某一行 (用行的序号):

import csv

with open('A.csv','rb') as csvfile:

reader = csv.reader(csvfile)

for i,rows in enumerate(reader):

if i == 2:

row = rows

print row

得到

['2', 'Ben', '13', '97']

4.使用字典存储csv内容:

import csv

with open('A.csv','rb') as csvfile:

reader = csv.DictReader(csvfile)

column = [row for row in reader]

print column

得到

[{'Age': '12', 'No.': '1', 'Score': '98', 'Name': 'Apple'},

{'Age': '13', 'No.': '2', 'Score': '97', 'Name': 'Ben'},

{'Age': '14', 'No.': '3', 'Score': '96', 'Name': 'Celia'},

{'Age': '15', 'No.': '4', 'Score': '95', 'Name': 'Dave'}]

5.读取csv的某一列(用列的标题):

import csv

with open('A.csv','rb') as csvfile:

reader = csv.DictReader(csvfile)

column = [row['Age'] for row in reader]

print column

得到

['12', '13', '14', '15']

6.读取csv的某一行(用行的标题):

import csv

with open('A.csv','rb') as csvfile:

reader = csv.DictReader(csvfile)

for row in reader:

if row['Name']=='Ben':

print row

得到

{'Age': '13', 'No.': '2', 'Score': '97', 'Name': 'Ben'}

python怎么写CSV文件

读文件时,我们把csv文件读入列表中,写文件时会把列表中的元素写入到csv文件中。

list = ['1', '2','3','4']

out = open(outfile, 'w')

csv_writer = csv.writer(out)

csv_writer.writerow(list)

可能遇到的问题:直接使用这种写法会导致文件每一行后面会多一个空行。

解决办法如下:

out = open(outfile, 'w', newline='')

csv_writer = csv.writer(out, dialect='excel')

csv_writer.writerow(list)

参考如下:

在stackoverflow上找到了比较经典的解释,原来 python3里面对 str和bytes类型做了严格的区分,不像python2里面某些函数里可以混用。所以用python3来写wirterow时,打开文件不要用wb模式,只需要使用w模式,然后带上newline=''。

3down vote

In Python 2.X, it was required to open the csvfile with 'b' because the csv module does its own line termination handling.

In Python 3.X, the csv module still does its own line termination handling, but still needs to know an encoding for Unicode strings. The correct way to open a csv file for writing is:

outputfile=open("out.csv",'w',encoding='utf8',newline='')

encoding can be whatever you require, but newline='' suppresses text mode newline handling. On Windows, failing to do this will write file line endings instead of the correct . This is mentioned in the 3.X csv.reader documentation only, but csv.writer requires it as well.


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:适合小孩学的编程语言(小学生适合学什么编程语言)
下一篇:人工智能如何推动教育发展(人工智能如何应用于教育)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~