python-操作csv文件
import csv lv,er=[],[] #读 with open('date.csv') as mycsv: reader=csv.DictReader(mycsv) for row in reader: print row['difftype'],row['index'],row['type'],row['updatevalue'] print '===================' with open('date.csv') as mycsv: reader=[each for each in csv.DictReader(mycsv)] print reader for row in reader: print row['difftype'],row['index'],row['type'],row['updatevalue'] lv.append(row['difftype']) er.append(row['updatevalue']) print 'lv=',lv print 'er=',er print '|'.join(er) er[0],er[1]='dd','cc' print er print '|'.join(er)
date.csv文件内容:
difftype,index,type,updatevalue
SettleDateformaterr,0,str,99999000
SettleDatenull,0,str,
Amountnull,1,num,0
ActivePython 2.7.2.5 (ActiveState Software Inc.) based on Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> SettleDateformaterr 0 str 99999000 SettleDatenull 0 str Amountnull 1 num 0 =================== [{'index': '0', 'type': 'str', 'difftype': 'SettleDateformaterr', 'updatevalue': '99999000'}, {'index': '0', 'type': 'str', 'difftype': 'SettleDatenull', 'updatevalue': ''}, {'index': '1', 'type': 'num', 'difftype': 'Amountnull', 'updatevalue': '0'}] SettleDateformaterr 0 str 99999000 SettleDatenull 0 str Amountnull 1 num 0 lv= ['SettleDateformaterr', 'SettleDatenull', 'Amountnull'] er= ['99999000', '', '0'] 99999000||0 ['dd', 'cc', '0'] dd|cc|0 >>>