简体   繁体   中英

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

I have implemented a python dictionary which has SQL query & results.

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. Note: Python dictionaries are not odered. so colomns in each row may vary when additional queries (eg Q7,Q8,Q9...) are added to the dictionary.

('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.

So, eg with addition of Q7, the dictionary Looks like

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

and the csv file will look like

('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.

The csv file does not hold a regular pattern. In the end, I would like to plot a graph with all available Qs and their corresponding values. Where the Qs are missing in the row, program should assume value 0 for that date.

You just need to process your 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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