【Python学习】csv库
csv(Comma-Separated Values, 逗号分割值)是存储表格数据的常⽤⽂件格式。
它每⼀⾏都⽤⼀个换⾏符分隔,列与列之间⽤逗号分隔。
本地⽂件
Python的csv库可以⾮常简单地修改csv⽂件,甚⾄从零开始创建⼀个csv⽂件。
⽐如:
import csv
csvFile = open("../files/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()
其中writerow()⽤来写⼊⾏。
⽹络⽂件
对于⽹络上的csv⽂件,我们可以直接把⽂件读成⼀个字符串,然后转换成⼀个StringIO对象,使它具有⽂件的属性。⽐如说从⽹上获取Monty Python乐团的专辑列表,然后把每⼀⾏都打印到命令⾏⾥。
quest import urlopen
from io import StringIO
import csv
data = urlopen("pythonscraping/files/MontyPythonAlbums.csv").read().decode('ascii','ignore')
dataFile = StringIO(data)
csvReader = ader(dataFile)
for row in csvReader:
print("The album \""+row[0]+"\" was released in "+str(row[1]))
输出结果是这样的
The album "Name" was released in Year
The album "Monty Python's Flying Circus" was released in 1970
The album "Another Monty Python Record" was released in 1971
...
所以可以发现这个ader返回的csvReader对象是可迭代的,⽽且是⼀个Python的列表。
还有⼀个csv.DictReader函数,它返回的对象是⼀个Python的字典。
quest import urlopen
from io import StringIO
import csv
data = urlopen("pythonscraping/files/MontyPythonAlbums.csv").read().decode('ascii','ignore') dataFile = StringIO(data)
csvReader = csv.DictReader(dataFile)
python怎么读csv数据print(dictReader.fieldnames)
for row in dictReader:
print(row)
输出结果
['Name','Year']
{'Name':"Monty Python's Flying Circus",'Year':'1970'}
{'Name':"Another Monty Python Record",'Year':'1971'}
...
【参考】
[1]《Python⽹络数据采集》Ryan Mitchell
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论