Python把csv数据写入list和字典类型的变量脚本

  1. #coding=utf8  
  2. import csv   
  3. import logging  
  4.   
  5. logging.basicConfig(level=logging.DEBUG,  
  6.                 format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',  
  7.                 datefmt='%a, %d %b %Y %H:%M:%S',  
  8.                 filename='readDate.log',  
  9.                 filemode='w')  
  10.   
  11. ''''' 
  12. 该模块的主要功能,是根据已有的csv文件, 
  13. 通过readDataToDicl函数,把csv中对应的部分, 
  14. 写入字典中,每个字典当当作一条json数据 
  15. '''  
  16. class GenExceptData(object):  
  17.     def __init__(self):  
  18.         try:  
  19.             #存放csv中读取的数据  
  20.             self.mdbuffer=[]  
  21.             #打开csv文件,设置读的权限  
  22.             csvHand=open("20170510174450.csv","r")  
  23.             #创建读取csv文件句柄  
  24.             readcsv=csv.reader(csvHand)  
  25.             #把csv的数据读取到mdbuffer中  
  26.             for row in readcsv:  
  27.                     self.mdbuffer.append(row)    
  28.             #把数据穿件为为字典类型的  
  29.             #self.readDataToList()  
  30.             #保存文件  
  31.         except Exception,e:  
  32.             logging.error("Read Excel  error:"+e)   
  33.         finally:  
  34.             #关闭csv文件  
  35.             csvHand.close()  
  36.     
  37.     def readDataToList(self):  
  38.         try:  
  39.             #获取mdbuffer中的元素个数  
  40.             rowNumber=len(self.mdbuffer)  
  41.             #设置当前行号  
  42.             currentrow=1  
  43.             #设置json数据的属性值  
  44.             propertyJson={}  
  45.             #propertyJsonList=[]  
  46.             #count=0  
  47.             #读取列表中的元素     
  48.             dataList=[]    
  49.             try:   
  50.                 for row in range(1,rowNumber):  
  51.                     #创建一个临时变量用来存取一次循环的属性键值  
  52.                     temp={}  
  53.                      
  54.                     #获取列表中一个元素  
  55.                     item=self.mdbuffer[row]  
  56.                     #获取当前元素,当前元素代表的是每个  
  57.                     #事件起始的位置  
  58.                     currentItem=self.mdbuffer[currentrow]  
  59.                     #获取serviceId并进行解码  
  60.                     serviceId= currentItem[2].decode("gbk")  
  61.                     #获取属性并进行解码,把解码的值存入propertyName  
  62.                     propertyName=item[3].decode("gbk")  
  63.                     #获取属性值并进行解码,把解码的值存入propertyValue  
  64.                     propertyValue=item[4].decode("gbk")  
  65.                     try:  
  66.                         #判断埋点事件与serviceId是否相等  
  67.                         if item[0]==currentItem[0and item[2]==currentItem[2]:  
  68.                             #把serviceId方式字典propertyJson中  
  69.                             propertyJson["serviceId"]=serviceId   
  70.                             #把属性/值对放入temp字典中                                                   
  71.                             temp[propertyName]=propertyValue  
  72.                             #调用字典的update函数,把temp中的键值对  
  73.                             #添加到 propertyJson字典中  
  74.                             propertyJson.update(temp)  
  75.                             #使用continue,如果为if条件为true则循环执行if语句模块  
  76.                             continue    
  77.                         else:  
  78.                             #把行号设置为当前行  
  79.                             currentrow=row    
  80.                             #把当前的属性解码放入propertyName                      
  81.                             propertyName=currentItem[3].decode("gbk")  
  82.                             #把当前的属性值解码放入propertyName  
  83.                             propertyValue=currentItem[4].decode("gbk")  
  84.                             #把serviceId方式字典propertyJson中   
  85.                             propertyJson["serviceId"]=serviceId      
  86.                             #把属性/值对放入propertyJson字典中    
  87.                             propertyJson[propertyName]=propertyValue  
  88.                             #propertyJsonList.append(propertyJson)   
  89.                             dataList.append(propertyJson)  
  90.                             ''''' 
  91.                             在这说下: 
  92.                             propertyJson.clear()与propertyJson={}的区别: 
  93.                             propertyJson.clear()是删除字典的值,不创建引用,会改变字典本身的值; 
  94.                             propertyJson={}是创建新的引用,字典的中的值不发现变化; 
  95.                             如果想让 self.dataDic.append(propertyJson)该语句执行成功,而且添加每次循环的值, 
  96.                             需要使用propertyJson={}方法; 
  97.                             如果使用propertyJson.clear(),只会把最后一次propertyJson存储的值,添加到self.dataDic中 
  98.                             '''  
  99.                             propertyJson={}  
  100.                     except Exception,e:  
  101.                         logging.error("Get Property Json Error:"+e)   
  102.                         print "Get Property Json Error:",e  
  103.             except Exception,e:  
  104.                 logging.error("Get Date Error:"+e)   
  105.                 print "Get Date Error:",e  
  106.   
  107.             return  dataList       
  108.         except Exception,e:  
  109.             logging.error("Reading Data TO Dic Error:"+e)   
  110.             print "Reading Data TO Dic Error:",e  
  111.           
  112.     def getAllServiceId(self):  
  113.         try:  
  114.             dataList=self.readDataToList()  
  115.             serList=[item["serviceId"for item in dataList if item["serviceId"] ]   
  116.             serList=list(set(serList))  
  117.             return serList  
  118.         except Exception,e:  
  119.             logging.error("Create ServiceId List Error:"+e)  
  120.             print "Create ServiceId List Error:"+e  
  121.                                           
  122.     def oupPutData(self):  
  123.         try:  
  124.             dataList=self.readDataToList()  
  125.             for item in dataList:            
  126.                     print "{"     
  127.                     for key,val in item.items():   
  128.                         print key,":",val  
  129.                     print "}"  
  130.                     print "#"*50  
  131.         except Exception,e:  
  132.             logging.error("OutPut Data Error:"+e)  
  133.             print "OutPut Data  Error:"+e  
  134.       
  135.         
  136.     def createDataDic(self):  
  137.         try:  
  138.             dataDic={}  
  139.              
  140.             dataList=self.readDataToList()  
  141.             count=0  
  142.             for item in dataList:  
  143.                 if item["serviceId"]==u"pageview":  
  144.                     count+=1  
  145.             print count  
  146.                       
  147.             serviceIdList=self.getAllServiceId()  
  148.             if len(serviceIdList)>0 and len(dataList)>0:  
  149.                 for serviceId in serviceIdList:  
  150.                     sameServiceidJosnList=[]  
  151.                     for item in dataList:                      
  152.                         itemServiceId=item["serviceId"]  
  153.                         if itemServiceId:  
  154.                             if serviceId==itemServiceId:   
  155.                                 sameServiceidJosnList.append(item)                                                                
  156.                         else:  
  157.                             print "ServiceId is null"  
  158.                     dataDic[serviceId]=sameServiceidJosnList   
  159.                       
  160.             else:  
  161.                 print "seriviceIdList  or dataList is null"  
  162.             return dataDic  
  163.             '''''   
  164.             for key,val in dataDic.items(): 
  165.                 print key,len(val) 
  166.                 print "*"*50 
  167.                 for item in val: 
  168.                     print "{" 
  169.                     for ke,va in item.items(): 
  170.                         print ke,":",va 
  171.                     print "}" 
  172.                 print "-"*50 
  173.             '''  
  174.         except Exception,e:  
  175.             print "Create Data Dictionary Error:",e    
  176.           
  177. def test():  
  178.     gen =GenExceptData()  
  179.     gen.oupPutData()  
  180.       
  181. if __name__=="__main__":  
  182.     test()  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值