CSV文件读和写

来源:转载


把数据存储到CSV

CSV(Comma-Separated Values, 逗号分隔值)是存储表哥数据的常用文件格式
从零开始创建一个CSV文件:


import csv
csvFile = open("../test.csv","w+")
try:
writer =csv.writer(csvFile)
writer.writerow(('number','number plus 2','number times 2'))
for i in range(10):
writer.writerow((i,i+2,i*2))
finally:
csvFile.close()

Python新建文件的机制考虑得非常周到,如果文件不存在,Python会自动创建文件(不会自动创建文件夹)。如果文件已经存在,Python会用新的数据覆盖之前的文件


获取HTML表格并写入CSV文件
import csv
from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://xinsichen.com")
bsObj = BeautifulSoup(html,"lxml")
table = bsObj.findAll("table",{"class":"table table-striped table-bordered"})[0]
rows = table.findAll("tr")
csvFile = open("../editors.csv","wt",newline='',encoding='utf-8')
writer = csv.writer(csvFile)
try:
for row in rows:
csvRow = []
for cell in row.findAll(['td','th']):
csvRow.append(cell.get_text())
writer.writerow(csvRow)
finally:
csvFile.close()
读取CSV
import csv
from io import StringIO
data = open("../editors.csv").read()
dataFile = StringIO(data)
csvReader = csv.reader(dataFile)
for row in csvReader:
print(row)


分享给朋友:
您可能感兴趣的文章:
随机阅读: