python csv 简单操作
# -*- coding: utf-8 -*- import csv data1 = [[‘name‘, ‘age‘, ‘sex‘], [‘张三‘, ‘19‘, ‘男‘], [‘李四‘, ‘22‘, ‘男‘], [‘王五‘, ‘20‘, ‘男‘], [‘赵六‘, ‘18‘, ‘女‘]] # writer with open(‘csvFile1.csv‘, ‘w‘) as file: # 解决中间有空行,也可以在open函数中加newline=‘‘解决 writer = csv.writer(file, lineterminator=‘\n‘) writer.writerows(data1) # reader with open(‘csvFile1.csv‘) as file: reader = csv.reader(file) for row in reader: print(row) # DicReader data2 = [] with open(‘csvFile1.csv‘) as file: dic_reader = csv.DictReader(file) for row in dic_reader: print(row) data2.append(row) # DicWriter header_data = [‘name‘, ‘age‘, ‘sex‘] with open(‘csvFile2.csv‘, ‘w‘, newline=‘‘) as file: dic_writer = csv.DictWriter(file, fieldnames=header_data) dic_writer.writeheader() for row in data2: dic_writer.writerow(row)
参考连接
相关推荐
夜斗不是神 2020-11-17
huavhuahua 2020-11-20
Yasin 2020-11-16
xiaoseyihe 2020-11-16
千锋 2020-11-15
diyanpython 2020-11-12
chunjiekid 2020-11-10
wordmhg 2020-11-06
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16