CSVs in Python
2016-07-22 20:34
260 查看
#Representing a CSV as a list of rows
#Option 1: Each row is a list
csv=[['A1','A2','A3'],
['B1','B2','B3']]
#Option 2:Each row is a dictionary
csv=[{'name1':'A1','name2':'A2','name3','A3'},
{'name1':'B1','name2':'B2','name3':'B3'}]
#to read in the student enrollments and print out the first record
########the first way
import unicodecsv
enrollments=[] ##first,I creat a list of enrollments
f=open('enrollments.csv','rb') ## Then,I open the file.The mode,rb,here,means that the file will be opened for reading,and the b flag changes the
format of how the file is read.The CSV documentation page mentions that I need to use this when i use this library
reader=unicodecsv.DictReader(f) ###DictReader which means each row will be a dictionary,I chose this since our data does have a header row,and
this will allow me to refer to each colum by its name,rather than its number.Now the reader won't actually be a list of rows.
instead it will be something called interator.---is that the interator let's you writer a for loop to access each element,but only once
for row in reader:
enrollments.append(row)
f.close()
enrollments[0]
############the second way
import unicodecsv
with open('enrollments.csv','rb') as f:
reader=unicodecsv.DictReader(f)
enrollments=list(reader)
enrollments[0]
#Option 1: Each row is a list
csv=[['A1','A2','A3'],
['B1','B2','B3']]
#Option 2:Each row is a dictionary
csv=[{'name1':'A1','name2':'A2','name3','A3'},
{'name1':'B1','name2':'B2','name3':'B3'}]
#to read in the student enrollments and print out the first record
########the first way
import unicodecsv
enrollments=[] ##first,I creat a list of enrollments
f=open('enrollments.csv','rb') ## Then,I open the file.The mode,rb,here,means that the file will be opened for reading,and the b flag changes the
format of how the file is read.The CSV documentation page mentions that I need to use this when i use this library
reader=unicodecsv.DictReader(f) ###DictReader which means each row will be a dictionary,I chose this since our data does have a header row,and
this will allow me to refer to each colum by its name,rather than its number.Now the reader won't actually be a list of rows.
instead it will be something called interator.---is that the interator let's you writer a for loop to access each element,but only once
for row in reader:
enrollments.append(row)
f.close()
enrollments[0]
############the second way
import unicodecsv
with open('enrollments.csv','rb') as f:
reader=unicodecsv.DictReader(f)
enrollments=list(reader)
enrollments[0]
相关文章推荐
- Dropbox 用什么语言开发的?(Python在各个平台都是全能的,特别是有PyObjC真没想到)
- Python.Extracting Data from XML
- Python.Following Links in HTML Using BeautifulSoup
- python使用urllib2抓取防爬取链接
- python-pickle/cPickle/glob/tarfile
- 转载:python使用urllib2抓取防爬取链接
- Numpy 常用函数及读写操作
- Python 关于正负无穷float(‘inf’)的一些用法
- Python之urlparse模块
- python中ascii码和字符的转换
- 利用python如何刷访问量
- python 之编码转换 unicode, utf-8, utf-16, GBK
- python 多线程爬虫
- 记一次安装Ipython的流程
- Python sort系列
- python排序函数sort()、sorted()、argsort()
- 文本处理(python)
- Scrapy:Python的爬虫框架----原理介绍
- python——异常处理
- Python中向excel中写入数据