1.利用csv包
import csv
#写
f=open("file.csv",'w',encoding='utf-8')
writer=csv.writer(f)
writer.writerows(" ")
f.close()
#读
f=open("file.csv",'r',encoding='utf-8')
reader = csv.reader(f)
f.close()
2.利用pandas
import pandas as pd
#写
dataframe.to_csv("file.csv",index=False)
#读
data = pd.read_csv("file.csv")index表示是否写入dataframe中的index(行名),默认为True
本文介绍了使用Python的csv包和pandas库进行CSV文件的读写操作方法。包括利用csv包的基本写入与读取过程,以及通过pandas进行更高效的数据框处理技巧。特别指出,在使用pandas写入数据时,可通过设置index参数来控制是否包含DataFrame的索引。
4万+

被折叠的 条评论
为什么被折叠?



