简体   繁体   English

python,在csv文件中存储和读取不同的字典大小信息

[英]python, Storing and Reading varying dictionary size information in a csv file

I have implemented a python dictionary which has SQL query & results. 我已经实现了一个python字典,它有SQL查询和结果。

logtime = time.strftime("%d.%m.%Y)
sqlDict = {             'time':logtime,
            'Q1' : 50,
            'Q2' : 15,
            'Q3' : 20,
            'Q4' : 10,
            'Q5' : 30,
}

Each day, the results are written in a CSV file in dictionary Format. 每天,结果都以字典格式写入CSV文件。 Note: Python dictionaries are not odered. 注意:Python字典不是odered。 so colomns in each row may vary when additional queries (eg Q7,Q8,Q9...) are added to the dictionary. 因此,当其他查询(例如Q7,Q8,Q9 ...)添加到字典时,每一行的列可能会有所不同。

('Q1', 25);('Q3', 23);('Q2', 15);('Q5', 320);('Q4', 130);('time', '20.03.2016')

('Q1', 35);('Q2', 21);('Q3', 12);('Q5', 30);('Q4', 10);('time', '21.03.2016')

('Q4', 22);('Q3', 27);('Q2', 15);('Q5', 30);('Q1', 10);('time', '22.03.2016')

With addition of a new SQL query in the dictionary, the additional Information is also saved in the same csv file. 通过在字典中添加新的SQL查询,其他信息也将保存在同一csv文件中。

So, eg with addition of Q7, the dictionary Looks like 因此,例如,加上Q7,字典看起来像

sqlDict = { 'time':logtime,
            'Q1' : 50,
            'Q2' : 15,
            'Q3' : 20,
            'Q4' : 10,
            'Q5' : 30,
            'Q7' : 5,
}

and the csv file will look like 和csv文件看起来像

('Q1', 25);('Q3', 23);('Q2', 15);('Q5', 320);('Q4', 130);('time', '20.03.2016') ('Q1',25);('Q3',23);('Q2',15);('Q5',320);('Q4',130);('time','20 .03.2016' )

('Q1', 35);('Q2', 21);('Q3', 12);('Q5', 30);('Q4', 10);('time', '21.03.2016')

('Q4', 22);('Q3', 27);('Q2', 15);('Q5', 30);('Q1', 10);('time', '22.03.2016')

('Q1', 50);('Q3', 20);('Q2', 15);('Q5', 30);('Q4', 10);('time', '23.03.2016');('Q7', 5)

I Need to plot all the Information available in the csv, ie for all SQL keys, the time vs value(numbers) plot. 我需要绘制csv中可用的所有信息,即所有SQL键,时间与值(数字)图。

The csv file does not hold a regular pattern. csv文件不包含常规模式。 In the end, I would like to plot a graph with all available Qs and their corresponding values. 最后,我想绘制一个包含所有可用Q及其对应值的图形。 Where the Qs are missing in the row, program should assume value 0 for that date. 如果行中缺少Q,程序应该假定该日期的值为0。

You just need to process your csv. 你只需要处理你的csv。 You know that the last cell of every row is the date so it's pretty formated for me. 您知道每行的最后一个单元格是日期,因此对我来说它的格式很漂亮。

import csv

with open("file.csv","r") as f:
    spamreader = csv.reader(f,delimiter=";")
    for row in spamreader:
        for value in range(len(row)):
            query,result = value.strip('(').strip(')').split(",")
            if query != "time":
                # process it
                # query = 'QX'
                # result = 'N'
            else:
                # query = 'time'
                # result = 'date'

The thing that will bother you is that you will read everything as string, so you will have to split on the coma and strip the '(' and the ')' 困扰你的是你会把所有东西都读成字符串,所以你必须分开昏迷并剥去'('和')'

for example: 例如:

query,result = row[x].strip('(').strip(')').split(", ")

then query = 'Q2' and result = 15 (type = string for both) 然后查询='Q2',结果= 15(两者都是type = string)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM