csv文件处理
读取csv文件:
import csv
with open('stock.csv','r') as fp:
reader = csv.reader(fp)
titles = next(reader)
for x in reader:
print(x)
这样操作,以后获取数据的时候,就要通过下表来获取数据。如果想要在获取数据的时候通过标题来获取。那么可以使用DictReader。示例代码如下:
import csv
with open('stock.csv','r') as fp:
reader = csv.DictReader(fp)
for x in reader:
print(x['turnoverVol'])
写入数据到csv文件:
写入数据到csv文件,需要创建一个writer对象,主要用到两个方法。一个是writerow,这个是写入一行。一个是writerows,这个是写入多行。示例代码如下:
import csv
headers = ['name','age','classroom']
values = [
('zhiliao',18,'111'),
('wena',20,'222'),
('bbc',21,'111')
]
with open('test.csv','w',newline='') as fp:
writer = csv.writer(fp)
writer.writerow(headers)
writer.writerows(values)
也可以使用字典的方式把数据写入进去。这时候就需要使用DictWriter了。示例代码如下:
import csv
headers = ['name','age','classroom']
values = [
{"name":'wenn',"age":20,"classroom":'222'},
{"name":'abc',"age":30,"classroom":'333'}
]
with open('test.csv','w',newline='') as fp:
writer = csv.DictWriter(fp,headers)
writer = csv.writeheader()
writer.writerow({'name':'zhiliao',"age":18,"classroom":'111'})
writer.writerows(values)