脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python把对应格式的csv文件转换成字典类型存储脚本的方法

Python把对应格式的csv文件转换成字典类型存储脚本的方法

2021-05-27 00:21坏蛋是我 Python

今天小编就为大家分享一篇Python把对应格式的csv文件转换成字典类型存储脚本的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

该脚本是为了结合之前的编写的脚本,来实现数据的比对模块,实现数据的自动化!由于数据格式是定死的,该代码只做参考,有什么问题可以私信我!

csv的数据格式截图如下:

Python把对应格式的csv文件转换成字典类型存储脚本的方法

readdatatodic.py源代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#coding=utf8
import csv
'''
该模块的主要功能,是根据已有的csv文件,
通过readdatatodicl函数,把csv中对应的部分,
写入字典中,每个字典当当作一条json数据
'''
class genexceptdata(object):
  def __init__(self):
    try:
      #用来存放json数据的字典
      self.datadic={}
      #存放csv中读取的数据
      self.mdbuffer=[]
      #打开csv文件,设置读的权限
      csvhand=open("20170510174450.csv","r")
      #创建读取csv文件句柄
      readcsv=csv.reader(csvhand)
      #把csv的数据读取到mdbuffer中
      for row in readcsv:
          self.mdbuffer.append(row)
      #把数据穿件为为字典类型的
      self.readdatatodicl()
      #保存文件
    except exception,e:
      print "read excel error:",e
    finally:
      #关闭csv文件
      csvhand.close()
 
  def readdatatodicl(self):
    try:
      #获取mdbuffer中的元素个数
      rownumber=len(self.mdbuffer)
      #设置当前行号
      currentrow=1
      #设置json数据的属性值
      propertyjson={}
      #读取列表中的元素  
      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")
        #判断埋点事件与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
          #输入字典中的值,并对值进行解码
          #该部分用于调试使用
          for key,val in propertyjson.items():
            print key,"=",val.encode("utf8")
          print "#"*50
          #为下次做准备,清除字典中的元素
          propertyjson.clear()
               
    except exception,e:
      print "reading data to dic error:",e
    
def test():
  genexceptdata()
  
if __name__=="__main__":
  test()

运行结果图:

Python把对应格式的csv文件转换成字典类型存储脚本的方法

以上这篇python把对应格式的csv文件转换成字典类型存储脚本的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/henni_719/article/details/74930674

延伸 · 阅读

精彩推荐