Python把csv文件中的信息写入字典中脚本(尝试)
2017-07-21 00:00
676 查看
该段代码,只供参考,与期望不符合,后期会附上,完成的脚本源码!
#coding=utf8 import csv class GenExceptData(object): def __init__(self): try: self.dataDic={} self.mdbuffer=[] #打开工作薄 csvHand=open("20170510174450.csv","r") readcsv=csv.reader(csvHand) for row in readcsv: self.mdbuffer.append(row) #把数据按 按照相应格式写入excel表中 self.readDataToDicl() #保存文件 except Exception,e: print "Read Excel error:",e finally: csvHand.close() def readDataToDicl(self): try: rowNumber=len(self.mdbuffer) currentrow=1 propertyJson={} for row in range(1,rowNumber): temp={} item=self.mdbuffer[row] currentItem=self.mdbuffer[currentrow] mdEvent= currentItem[0].decode("gbk") serviceId= currentItem[2].decode("gbk") propertyName=item[3].decode("gbk") propertyValue=item[4].decode("gbk") if item[0]==currentItem[0] and item[2]==currentItem[2]: print row , currentrow,mdEvent,serviceId,propertyName,propertyValue temp["serviceId"]=serviceId temp[propertyName]=propertyValue propertyJson.update(temp) temp.clear() continue else: currentrow=row currentItem=self.mdbuffer[currentrow+1] mdEvent= currentItem[0].decode("gbk") serviceId= currentItem[2].decode("gbk") proName=currentItem[3].decode("gbk") proValue=currentItem[4].decode("gbk") temp["serviceId"]=serviceId temp[proName]=proValue print row , currentrow,mdEvent,serviceId,propertyName,propertyValue propertyJson.update(temp) temp.clear() continue break for key,val in propertyJson.items(): print key,"=",val.encode("utf8") print "*"*50 self.dataDic[mdEvent]=propertyJson ''' for value in self.dataDic.values(): for key,val in value.items(): print key,"=",val.encode("utf8") print "*"*50 ''' except Exception,e: print "Reading Data TO Dic Error:",e def test(): GenExceptData() if __name__=="__main__": test()
相关文章推荐
- Python把csv文件中的信息写入字典中脚本(尝试)
- Python把csv数据写入list和字典类型的变量脚本
- python 实现将字典写入csv文件
- Python把csv数据写入list和字典类型的变量脚本
- 写个python脚本,处理bitmap.fnt文件,将信息存放入.csv文件中
- Python把csv数据写入list和字典类型的变量脚本方法
- Python用csv写入文件_消除空余行的方法
- python把csv数据做成列表、字典类型的数据进行存储脚本(readDataToDic_V2.2)
- python csv文件写入
- 用Python将gml文件中边的信息输出为csv(或者txt)格式
- Python爬虫小实践:寻找失踪人口,爬取失踪儿童信息并写成csv文件,方便存入数据库
- Python3 将信息写入txt文件时的编码问题 ™
- python写入csv文件中文乱码问题解决办法
- CSV文件的使用-写入/写出(C++/Python)
- python读文件保存到字典,修改字典并写入新文件
- 用python 读取和写入CSV格式的文件
- Python爬虫小实践:寻找失踪人口,爬取失踪儿童信息并写成csv文件,方便存入数据库
- python 写入csv文件
- python 把一个文本文件中的语句分词, 并去重,然后写入一个CSV文件后,你可以排序
- python在处理CSV文件时,字符串和列表写入的区别