标签:文件 color ade print main 包含 pen 读取 class
1 """读取csv文件""" 2 3 4 import csv 5 6 def readcsv_demo1(): 7 """采用列表形式,下标操作""" 8 with open(‘csvwriter.csv‘, ‘r‘) as fp: 9 # reader是一个迭代器 10 reader = csv.reader(fp) 11 next(reader) #向下走一行 12 for x in reader: 13 #print(x) 14 name = x[1] 15 value = x[-1] 16 print({‘name‘:name, ‘value‘:value}) 17 fp.close() 18 19 def readcsv_demo2(): 20 """采用字典形式读取""" 21 with open(‘csvwriter.csv‘, ‘r‘, encoding=‘utf-8‘) as fp: 22 # 不会包含标题的数据 23 reader = csv.DictReader(fp) 24 for x in reader: 25 print(x) 26 print(x[‘username‘]) # 字典操作取某个值 27 fp.close() 28 29 if __name__ == "__main__": 30 readcsv_demo1() 31 readcsv_demo2()
标签:文件 color ade print main 包含 pen 读取 class
原文地址:https://www.cnblogs.com/sruzzg/p/13138208.html