Python-csv模块读写csv文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python-csv模块读写csv文件相关的知识,希望对你有一定的参考价值。
import csv
# 采用DictReader进行读写:
# 读csv文件
def get_data(self, from_file):
test_data = []
with open(from_file, ‘rb‘) as csv_file:
csv.register_dialect(‘read‘, delimiter=‘\t‘, quoting=csv.QUOTE_NONE)
reader = csv.DictReader(csv_file, dialect=‘read‘)
for row in reader:
test_data.append(row)
return test_data
# 写csv文件
def set_data(self, to_file, field_names, data):
with open(to_file, ‘wb‘) as csv_file:
csv.register_dialect(‘write‘, quoting=csv.QUOTE_ALL)
writer = csv.DictWriter(csv_file, fieldnames=field_names, dialect=‘write‘)
writer.writeheader()
for row in data:
writer.writerow(row)
以上是关于Python-csv模块读写csv文件的主要内容,如果未能解决你的问题,请参考以下文章