李守中

Python csv 文件读写

Table of Contents

1. 读取 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'])

2. 写入数据到 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)


Last Update: 2023-05-18 Thu 09:12

Generated by: Emacs 28.2 (Org mode 9.5.5)   Contact: [email protected]

若正文中无特殊说明,本站内容遵循: 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议