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)
原文地址:https://www.cnblogs.com/caojunjie/p/6827804.html