csv文件读取
Posted Braveliberty
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csv文件读取相关的知识,希望对你有一定的参考价值。
1 from urllib.request import urlopen 2 from io import StringIO 3 import csv 4 5 data = urlopen("http://pythonscraping.com/files/MontyPythonAlbums.csv").read().decode(‘ascii‘, ‘ignore‘) 6 dataFile = StringIO(data) 7 csvReader = csv.reader(dataFile) 8 9 for row in csvReader: 10 print("The album \""+row[0]+"\" was released in "+str(row[1]))
上边是读取结果为列表形式,下边用dictReader()读取城字典形式
1 from urllib.request import urlopen 2 from io import StringIO 3 import csv 4 5 data = urlopen("http://pythonscraping.com/files/MontyPythonAlbums.csv").read().decode(‘ascii‘, ‘ignore‘) 6 dataFile = StringIO(data) 7 dictReader = csv.DictReader(dataFile) 8 9 print(dictReader.fieldnames) 10 11 for row in dictReader: 12 print(row)
以上是关于csv文件读取的主要内容,如果未能解决你的问题,请参考以下文章