在使用python处理数据的过程中,经常需要做一些数据读取和写入的工作,比较常用的数据格式是csv,csv文件是一种以逗号分割字符的文件形式
例如:demo.csv
,一个很简单的csv文件
name,score
alex,1
jon,2
sansa,3
读写csv文件常用的有两种方式,一种是使用csv
,一种是使用pandas
1 使用pandas进行读写
读
pandas 是一个非常好用的工具,能够按照格式读入和方便的写出csv文件,下面是一个简单的读文件的例子:
import pandas as pd
file = pd.read_csv('demo.csv')
df = pd.DataFrame(file)
print(df)
output:
title score 0 alex 1 1 jon 2 2 sansa 3
如果想要一行一行的读取dataframe中的内容:
import pandas as pd
file = pd.read_csv('demo.csv')
df = pd.DataFrame(file)
for i in range(len(df)):
document = df[i:i+1]
print(document,'\n')
output:
title score
0 alex 1
title score
1 jon 2
title score
2 sansa 3
要取到document中的每个值:
for i in range(len(df)):
document = df[i:i+1]
title = document['title'][i]
score = document['score'][i]
print(title,score,'\n')
写
使用pandas将dictionary写入csv文件,按列写入
import pandas as pd
file = pd.read_csv('demo.csv')
df = pd.DataFrame(file)
dict = {}
for i in range(len(df)):
document = df[i:i+1]
title = document['title'][i]
score = document['score'][i]
dict[title] = score
new_df = pd.DataFrame.from_dict(dict,orient='index')
new_df.to_csv('pandas_new.csv')
结果文件pandas_new.csv
:
,0
alex,1
jon,2
sansa,3
Plus:一个不错的介绍链接
2 使用csv进行读写
读
import csv
with open('demo.csv', 'r') as csvfile:
spamreader = csv.reader(csvfile)
for row in spamreader:
print(row)# type of row: list
output:
['title,score'] ['alex,1'] ['jon,2'] ['sansa,3']
Hints:
When you read the csv file, if you use ‘rb’, means Read the file in Binary mode.
while if you use ‘rt’, means Read the file in Text mode.
I usually just use ‘r’.
写
import csv
with open('new.csv','w') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['country','language'])#这里要以list形式写入,writer会在新建的csv文件中,一行一行写入
output:
csv文件,content:
country,language
Plus:如果想要以dictionary的形式读取和写入,csv也提供了DictReader,DIctWriter的方法。