码迷,mamicode.com
首页 > 其他好文 > 详细

csv文件读取

时间:2017-05-08 21:59:29      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:class   was   nal   urlopen   import   log   dict   com   sed   

 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文件读取

标签:class   was   nal   urlopen   import   log   dict   com   sed   

原文地址:http://www.cnblogs.com/caojunjie/p/6827804.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!