把CSV数据合成json样书存入字典、列表并打印的python脚本

来源:互联网 发布:谁有微信淘宝免单群 编辑:程序博客网 时间:2024/05/17 05:54

1.脚本实现背景

该脚本的目的是,为了把期望数据转换成json数据,为之前代理脚本获取的实时数据进行对比!为实现数据自动化对比做铺垫!

原有的数据csv中数据格式如下所示:


上图中颜色圈起来的部分作为一个数据项,会整理整格式如下所示的:


2.脚本源代码

脚本名是readDataToDic.py

#coding=utf8import csv import logging'''Author:ewangData:2017/07/12该模块的主要功能函数:readDataToList():把csv中的数据,数据项以字典类型存储在列表中。getAllServiceId():获取所有的serviceId列表printListData():输出数据list中的每项数据createDataDic():创建一个数据字典表,以serviceId为key,相同的数据项列表为valueprintDicData():输出数据字典中的每项数据'''logging.basicConfig(level=logging.DEBUG,                format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',                datefmt='%a, %d %b %Y %H:%M:%S',                filename='readDate.log',                filemode='w')class GenExceptData(object):    def __init__(self):        try:            #存放csv中读取的数据            self.mdbuffer=[]            #打开csv文件,设置读的权限            csvHand=open("20170510174450.csv","r")            #创建读取csv文件句柄            readcsv=csv.reader(csvHand)            #把csv的数据读取到mdbuffer中            for row in readcsv:                    self.mdbuffer.append(row)              #把数据穿件为为字典类型的        except Exception,e:            logging.error("Read Excel  error:"+e)         finally:            #关闭csv文件            csvHand.close()      def readDataToList(self):        try:            #在数组最后添加一个空白行            #该行的作用是为了成功获取最后一条json数据            #在数组endLine添加空白字符            endLine=[" " for num in range(len(self.mdbuffer[1])) if num>=0]             #把以空字符的endLine添加到末尾            self.mdbuffer.append(endLine)            #获取mdbuffer中的元素个数            rowNumber=len(self.mdbuffer)            #设置当前行号            currentrow=1            #设置json数据的属性值            propertyJson={}            #读取列表中的元素               dataList=[]              try:                 for row in range(1,rowNumber):                    #创建一个临时变量用来存取一次循环的属性键值                    temp={}                    #获取列表中一个元素                    item=self.mdbuffer[row]                    #获取当前元素,当前元素代表的是每个                    #事件起始的位置                    currentItem=self.mdbuffer[currentrow]                    #获取serviceId并进行解码                    serviceId= currentItem[2].decode("gbk")                    #获取属性并进行解码,把解码的值存入propertyName                    propertyName=item[3].decode("gbk")                    #获取属性值并进行解码,把解码的值存入propertyValue                    propertyValue=item[4].decode("gbk")                    try:                        #判断埋点事件与serviceId是否相等                        if item[0]==currentItem[0] and item[2]==currentItem[2]:                            #把serviceId方式字典propertyJson中                            propertyJson["serviceId"]=serviceId                             #把属性/值对放入temp字典中                                                                             temp[propertyName]=propertyValue                            #调用字典的update函数,把temp中的键值对                            #添加到 propertyJson字典中                            propertyJson.update(temp)                            #使用continue,如果为if条件为true则循环执行if语句模块                            continue                          else:                            #把行号设置为当前行                            currentrow=row                              #把当前的属性解码放入propertyName                                                propertyName=currentItem[3].decode("gbk")                            #把当前的属性值解码放入propertyName                            propertyValue=currentItem[4].decode("gbk")                            #把serviceId方式字典propertyJson中                             propertyJson["serviceId"]=serviceId                                #把属性/值对放入propertyJson字典中                              propertyJson[propertyName]=propertyValue                            #propertyJsonList.append(propertyJson)                             dataList.append(propertyJson)                            '''                            在这说下:                            propertyJson.clear()与propertyJson={}的区别:                            propertyJson.clear()是删除字典的值,不创建引用,会改变字典本身的值;                            propertyJson={}是创建新的引用,字典的中的值不发现变化;                            如果想让 self.dataDic.append(propertyJson)该语句执行成功,而且添加每次循环的值,                            需要使用propertyJson={}方法;                            如果使用propertyJson.clear(),只会把最后一次propertyJson存储的值,添加到self.dataDic中                            '''                            propertyJson={}                    except Exception,e:                        logging.error("Get Property Json Error:" +e)                         #print "Get Property Json Error:",e            except Exception,e:                logging.error("Get Date Error:"+e)                 #print "Get Date Error:",e            #返回dataList            return  dataList             except Exception,e:            #把信息写入日志中            logging.error("Reading Data TO Dic Error:"+e)             #print "Reading Data TO Dic Error:",e            def getAllServiceId(self):        try:            #调用readDataToList函数创建一个数据list            dataList=self.readDataToList()            #把数据list中的所有serviceId放入表serList中            serList=[item["serviceId"] for item in dataList if item["serviceId"] ]             #对serList中的数据去重,分为两步:            #第一步把列表转换成集合:set(serList)            #第二步:把集合转换为list:list(set(serList))            #集合和list的区别:集合中的数据是唯一性,不存在相同部分            serList=list(set(serList))            #返回serList            return serList        except Exception,e:            logging.error("Create ServiceId List Error:"+e)            #print "Create ServiceId List Error:"+e                #输出list中的数据信息                                      def printListData(self):        try:            #调用readDataToList方法获取dataList列表            dataList=self.readDataToList()            #对列表中的数据执行for循环            #并输出类似与json样式的数据            for item in dataList:                                                print "{"                       #输出键值对                    for key,val in item.items():                         print "\t",key,":",val,","                    print "}"                    #设置以#格式的分隔符                    print "#"*50        except Exception,e:            logging.error("OutPut  List Data  Error:"+e)            #print "OutPut  List Data  Error:"+e        #创建一个数据字典    def createDataDic(self):        try:            #定义个数据字典变量            dataDic={}                #调用函数readDataToList创建一个dataList表              dataList=self.readDataToList()               #调用getAllServiceId获取serviceId列表                       serviceIdList=self.getAllServiceId()            #判断列表中是否有元素,如果有执行if语句            if len(serviceIdList)>0 and len(dataList)>0:                #对serviceIdList进行循环,以serviceId作为key                for serviceId in serviceIdList:                    #创建一个list用来存放serviceId相同的数据项                    sameServiceidJosnList=[]                    #对数据列表执行循环                    for item in dataList:                        #获取字典中键为serviceId值,放入变量中                                          itemServiceId=item["serviceId"]                        #如果值不为空,执行if语句                        if itemServiceId:                            #判断serviceId与数据项中serviceId的值是否相等                            #如果相等执行if语句块                            if serviceId==itemServiceId:                                 #把数据项加入sameServiceidJosnList列表中                                sameServiceidJosnList.append(item)                                                                                      else:                            logging.debug("ServiceId is null")                            #print "ServiceId is null"                    #给字典赋值,以serviceId作为key,                    #serviceId相同的数据项列表作为值                    dataDic[serviceId]=sameServiceidJosnList                                 else:                logging.debug("seriviceIdList  or dataList is null")                #print "seriviceIdList  or dataList is null"            #返回字典类型的数据            return dataDic        except Exception,e:            logging.error("Create Data Dictionary Error:"+e)            #print "Create Data Dictionary Error:",e          #打印字典信息             def printDicData(self):        try:            #调用createDataDic创建dataDic字典            dataDic=self.createDataDic()            #对字典中的数据进行循环,获取键值对            for serviceId,dataitem in dataDic.items():                                          print "{"                       print "\t",serviceId,":","["                       #由于值是列表,列表中的数据项类型是字典类型                    #每一数据项是哈希表                    for item in dataitem:                        print "\t\t{"                        #输出哈希表中的数据                        for key,val in item.items():                             print "\t\t\t",key,":",val,","                        print "\t\t},"                    print"\t]"                    print "}\n"                    print "#"*50        except Exception,e:            logging.error("OutPut  Dictionary Data  Error:"+e)            #print "OutPut Dictionary Data  Error:"+edef test():    gen =GenExceptData()    gen.printDicData()    if __name__=="__main__":    test()

3.运行结果



阅读全文
0 0
原创粉丝点击