python CSV文献写入和读取

python CSV文献写入和读取

Python中导出CSV扩充包即可运用,囊括写入文献和读取文献。

csv文献的写入办法

导出CSV模块创造一个CSV文献东西写入CSV文献封闭文献

# -*- coding: utf-8 -*-

import csv

c = open("test-01.csv", "w", encoding="utf8") #写文献

writer = csv.writer(c)

writer.writerow(['序号','全名','年纪'])

tlist = []

tlist.append("1")

tlist.append("小明")

tlist.append("10")

writer.writerow(tlist)

print(tlist,type(tlist))

del tlist[:] #清空

tlist.append("2")

tlist.append("小红")

tlist.append("9")

writer.writerow(tlist)

print(tlist,type(tlist))

c.close()

CSV文献读取

导出CSV模块创造一个CSV文献东西读取CSV文献封闭文献

# -*- coding: utf-8 -*-

import csv

c = open("test-01.csv", "r", encoding="utf8") #读文献

reader = csv.reader(c)

for line in reader:

print(line[0],line[1],line[2])

c.close()

分享到 :
相关推荐

Leave a Reply

Your email address will not be published. Required fields are marked *