国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python把對應(yīng)格式的csv文件轉(zhuǎn)換成字典類型存儲腳本的方法

2020-02-23 06:26:29
字體:
供稿:網(wǎng)友

該腳本是為了結(jié)合之前的編寫的腳本,來實現(xiàn)數(shù)據(jù)的比對模塊,實現(xiàn)數(shù)據(jù)的自動化!由于數(shù)據(jù)格式是定死的,該代碼只做參考,有什么問題可以私信我!

CSV的數(shù)據(jù)格式截圖如下:

Python把對應(yīng)格式的csv文件轉(zhuǎn)換成字典類型存儲腳本

readDataToDic.py源代碼如下:

#coding=utf8import csv '''該模塊的主要功能,是根據(jù)已有的csv文件,通過readDataToDicl函數(shù),把csv中對應(yīng)的部分,寫入字典中,每個字典當(dāng)當(dāng)作一條json數(shù)據(jù)'''class GenExceptData(object):  def __init__(self):    try:      #用來存放json數(shù)據(jù)的字典      self.dataDic={}      #存放csv中讀取的數(shù)據(jù)      self.mdbuffer=[]      #打開csv文件,設(shè)置讀的權(quán)限      csvHand=open("20170510174450.csv","r")      #創(chuàng)建讀取csv文件句柄      readcsv=csv.reader(csvHand)      #把csv的數(shù)據(jù)讀取到mdbuffer中      for row in readcsv:          self.mdbuffer.append(row)       #把數(shù)據(jù)穿件為為字典類型的      self.readDataToDicl()      #保存文件    except Exception,e:      print "Read Excel error:",e    finally:      #關(guān)閉csv文件      csvHand.close()   def readDataToDicl(self):    try:      #獲取mdbuffer中的元素個數(shù)      rowNumber=len(self.mdbuffer)      #設(shè)置當(dāng)前行號      currentrow=1      #設(shè)置json數(shù)據(jù)的屬性值      propertyJson={}      #讀取列表中的元素         for row in range(1,rowNumber):        #創(chuàng)建一個臨時變量用來存取一次循環(huán)的屬性鍵值        temp={}        #獲取列表中一個元素        item=self.mdbuffer[row]        #獲取當(dāng)前元素,當(dāng)前元素代表的是每個        #事件起始的位置        currentItem=self.mdbuffer[currentrow]        #獲取serviceId并進(jìn)行解碼        serviceId= currentItem[2].decode("gbk")        #獲取屬性并進(jìn)行解碼,把解碼的值存入propertyName        propertyName=item[3].decode("gbk")        #獲取屬性值并進(jìn)行解碼,把解碼的值存入propertyValue        propertyValue=item[4].decode("gbk")        #判斷埋點事件與serviceId是否相等        if item[0]==currentItem[0] and item[2]==currentItem[2]:          #把serviceId方式字典propertyJson中          propertyJson["serviceId"]=serviceId           #把屬性/值對放入temp字典中                                   temp[propertyName]=propertyValue          #調(diào)用字典的update函數(shù),把temp中的鍵值對          #添加到 propertyJson字典中          propertyJson.update(temp)          #使用continue,如果為if條件為true則循環(huán)執(zhí)行if語句模塊          continue         else:          #把行號設(shè)置為當(dāng)前行          currentrow=row           #把當(dāng)前的屬性解碼放入propertyName                    propertyName=currentItem[3].decode("gbk")          #把當(dāng)前的屬性值解碼放入propertyName          propertyValue=currentItem[4].decode("gbk")          #把serviceId方式字典propertyJson中           propertyJson["serviceId"]=serviceId            #把屬性/值對放入propertyJson字典中           propertyJson[propertyName]=propertyValue           #輸入字典中的值,并對值進(jìn)行解碼          #該部分用于調(diào)試使用           for key,val in propertyJson.items():            print key,"=",val.encode("utf8")          print "#"*50           #為下次做準(zhǔn)備,清除字典中的元素          propertyJson.clear()                   except Exception,e:      print "Reading Data TO Dic Error:",e    def test():  GenExceptData()  if __name__=="__main__":  test()            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 霞浦县| 诸暨市| 望城县| 班玛县| 尼木县| 集贤县| 高密市| 诸暨市| 桃园县| 平南县| 铜山县| 乌鲁木齐县| 怀仁县| 报价| 青河县| 安义县| 赞皇县| 休宁县| 马尔康县| 德惠市| 金门县| 永城市| 合川市| 汕尾市| 焦作市| 舟山市| 贺兰县| 临海市| 平阳县| 诸暨市| 奎屯市| 武宣县| 宾川县| 吉安市| 鄂伦春自治旗| 滦平县| 侯马市| 佳木斯市| 沈丘县| 桓台县| 禹州市|