大家好,好长时间都没有更新博客,最近会梳理一下自己在工作中经常遇到的内容,尽量简单易懂,可以直接copy,运行起来,当然大家也可以收藏起来,以后直接copy使用
# coding: utf-8
import csv
'''
写读追加状态
'r':读
'w':写
'a':追加
'r+' == r+w(可读可写,文件若不存在就报错(IOError))
'w+' == w+r(可读可写,文件若不存在就创建)
'a+' ==a+r(可追加可写,文件若不存在就创建)
对应的,如果是二进制文件,就都加一个b就好啦:
'rb' 'wb' 'ab' 'rb+' 'wb+' 'ab+'
'''
# read csv file, return list
def read_csv(file_name):
with open(file_name, 'r',encoding='utf-8') as f:
reader = csv.reader(f)
data = list(reader)
return data
# write csv file, return nothing
def write_csv(file_name, data, mode='w+'):
with open(file_name, mode, newline="\n",encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerows(data)
# dict writ into csv file, return nothing
def write_csv_dict(file_name, data, mode='w+'):
with open(file_name, mode, newline="\n",encoding='utf-8') as f:
writer = csv.DictWriter(f, data[0].keys())
writer.writeheader()
writer.writerows(data)
# dict read from csv file, return list
def read_csv_dict(file_name,mode='r'):
with open(file_name, mode, newline="\n",encoding='utf-8') as f:
reader = csv.DictReader(f)
data = list(reader)
return data
if __name__ == '__main__':
# write csv
data1 = [['haha', '7', '山东'], ['草莓', '6', '徐州']]
write_csv('data.csv', data1, 'a')
data2 = read_csv('data.csv')
print(data2)
# read csv
data = read_csv('data.csv')
print(data)
# write csv dict
data3 = [{'name': 'haha', 'age': '7', 'address': '山东'}, {'name': '草莓', 'age': '6', 'address': '徐州'}]
write_csv_dict('data1.csv', data3, 'w+')
# read csv dict
data4 = read_csv_dict('data1.csv')
print(data4)
运行效果: