csv文件的读取写法 from Udacity

长版本

import unicodecsv
enrollments_filename = 'C:\\Users\\xxxxx\\Desktop\\try.csv'

enrollments = []
f = open(enrollments_filename, 'rb')  
reader = unicodecsv.DictReader(f)

for row in reader:
    enrollments.append(row)
print(enrollments)
f.close()        #需要关闭文件

简化版本

import unicodecsv
enrollments_filename = 'C:\\Users\\xxxxx\\Desktop\\try.csv'

with open(enrollments_filename, 'rb') as f:          #使用完后文件自动关闭
    reader = unicodecsv.DictReader(f)
    enrollments = list(reader)                #自动生成list
print(enrollments)
posted @ 2019-09-17 20:10  中庭之园  阅读(241)  评论(0编辑  收藏  举报