writing dictionary of dictionaries to .csv file in a particular format

2024/10/10 16:21:27

I am generating a dictionary out of multiple .csv files and it looks like this (example):

dtDict = {'AV-IM-1-13991730': {'6/1/2014 0:10': '0.96','6/1/2014 0:15': '0.92','6/1/2014 0:20': '0.97'},'AV-IM-1-13991731': {'6/1/2014 0:10': '1.96','6/1/2014 0:15': '1.92','6/1/2014 0:20': '1.97'},'AV-IM-1-13991732': {'6/1/2014 0:10': '2.96','6/1/2014 0:15': '2.92','6/1/2014 0:20': '2.97'},'AV-IM-1-13991733': {'6/1/2014 0:10': '3.96','6/1/2014 0:15': '3.96','6/1/2014 0:20': '3.97'}}

I want to save it to a .csv file in the following format:

timestamp,AV-IM-1-13991730,AV-IM-1-13991731,AV-IM-1-13991732,AV-IM-1-13991733
6/1/2014 0:10,0.96,1.96,2.96,3.96
6/1/2014 0:15,0.92,1.92,2.92,3.96
6/1/2014 0:20,0.97,1.97,2.97,3.97

The piece of code I have as of now (related to this objective):

header = '''# file...... Recorder file
# date...... Thu Mar 12 14:35:32 2015
# user...... Sri
# host...... (null)
# group..... None
# property.. AVA Measurements
# limit..... 
# interval..''' testpower        = open("custpower.csv",'w')
testpower.writelines([header,'\n','# timestamp\n'])
...
for key, value in dtDict.iteritems():#Still trying to figure out how to write to custpower.csv

I tried doing something similar to this:

for key, value in dtDict.iteritems():testpower.writelines([key,',',','.join(value),'\n'])

but it didnot quite do what I was trying to do.

Answer

This is beyond simple if you can use pandas.

import pandas as pddata = {'AV-IM-1-13991730': {'6/1/2014 0:10': '0.96','6/1/2014 0:15': '0.92','6/1/2014 0:20': '0.97'},'AV-IM-1-13991731': {'6/1/2014 0:10': '1.96','6/1/2014 0:15': '1.92','6/1/2014 0:20': '1.97'},'AV-IM-1-13991732': {'6/1/2014 0:10': '2.96','6/1/2014 0:15': '2.92','6/1/2014 0:20': '2.97'},'AV-IM-1-13991733': {'6/1/2014 0:10': '3.96','6/1/2014 0:15': '3.96','6/1/2014 0:20': '3.97'}}df = pd.DataFrame(data)
df.to_csv(PATH_TO_OUTPUT_FILE)

df becomes a DataFrame that looks like

              AV-IM-1-13991730 AV-IM-1-13991731 AV-IM-1-13991732 AV-IM-1-13991733
6/1/2014 0:10             0.96             1.96             2.96             3.96
6/1/2014 0:15             0.92             1.92             2.92             3.96
6/1/2014 0:20             0.97             1.97             2.97             3.97

And your resulting csv looks like

,AV-IM-1-13991730,AV-IM-1-13991731,AV-IM-1-13991732,AV-IM-1-13991733
6/1/2014 0:10,0.96,1.96,2.96,3.96
6/1/2014 0:15,0.92,1.92,2.92,3.96
6/1/2014 0:20,0.97,1.97,2.97,3.97

Pandas is also nice because you can then do:

df.convert_objects(convert_numeric=True).plot()
# the converts change "0.97" -> 0.97 so it's plottable

To get:

Dataframe

https://en.xdnf.cn/q/69877.html

Related Q&A

How to import SSL certificates for Firefox with Selenium [in Python]?

Trying to find a way to install a particular SSL certificate in Firefox with Selenium, using the Python WebDriver and FirefoxProfile. We need to use our own, custom certificate which is stored in the …

Cell assignment of a 2-dimensional Matrix in Python, without numpy

Below is my script, which basically creates a zero matrix of 12x8 filled with 0. Then I want to fill it in, one by one. So lets say column 2 row 0 needs to be 5. How do I do that? The example below sh…

Fill matplotlib subplots by column, not row

By default, matplotlib subplots are filled by row, not by column. To clarify, the commandsplt.subplot(nrows=3, ncols=2, idx=2) plt.subplot(nrows=3, ncols=2, idx=3)first plot into the upper right plot o…

Find the 2nd highest element

In a given array how to find the 2nd, 3rd, 4th, or 5th values? Also if we use themax() function in python what is the order of complexity i.e, associated with this function max()?.def nth_largest(…

pandas data frame - select rows and clear memory?

I have a large pandas dataframe (size = 3 GB):x = read.table(big_table.txt, sep=\t, header=0, index_col=0)Because Im working under memory constraints, I subset the dataframe:rows = calculate_rows() # a…

How do I format a websocket request?

Im trying to create an application in Python that powers a GPIO port when the balance of a Dogecoin address changes. Im using the websocket API here and this websocket client.My code looks like this:fr…

cherrypy and wxpython

Im trying to make a cherrypy application with a wxpython ui. The problem is both libraries use closed loop event handlers. Is there a way for this to work? If I have the wx ui start cherrypy is that g…

What is the logic behind d3.js nice() ticks

I have generated some charts in d3.js. I use the following code to calculate the values to put in my y axis which works like a charm.var s = d3.scale.linear().domain([minValue, maxValue]); var ticks = …

Changing iterable variable during loop

Let it be an iterable element in python. In what cases is a change of it inside a loop over it reflected? Or more straightforward: When does something like this work?it = range(6) for i in it:it.remo…

Are CPython, IronPython, Jython scripts compatible with each other?

I am pretty sure that python scripts will work in all three, but I want to make sure. I have read here and there about editors that can write CPython, Jython, IronPython and I am hoping that I am look…