Python把csv文件中的信息写入字典中脚本(尝试)
2017-07-07 17:30
573 查看
该段代码,只供参考,与期望不符合,后期会附上,完成的脚本源码!
#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()
#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脚本,处理bitmap.fnt文件,将信息存放入.csv文件中
- Python把csv数据写入list和字典类型的变量脚本
- Python把csv数据写入list和字典类型的变量脚本
- python 实现将字典写入csv文件
- python csv文件写入
- Python写入CSV文件的方法
- Python写入CSV文件的问题
- python 写入csv文件
- spl处理文件(文件详细信息、文件遍历、查询指定行、写入CSV文件)
- Python 获取磁盘信息的脚本及常用文件操作等
- 如何用idapython脚本获取字符串窗口的数据并写入文件
- python cvs文件处理脚本 python csv
- 转换vcf文件到csv文件的python脚本
- 使用Python读取和写入mp3文件的id3v1信息
- Python脚本---把MySQL数据库表中的数据导出生成csv格式文件
- 关于python3中写入csv文件多余空行
- python之模块csv之CSV文件的写入(按行写入)
- Python用csv写入文件---消除空余行的技巧