Home > Back-end >  After writing a dict to CSV , how do you revert this to retrieve the original dict
After writing a dict to CSV , how do you revert this to retrieve the original dict

Time:10-13

I have a python script which saves my dict into a csv

My dict has the following structure:

 dict_data= dict()
 
 xplanes=[7,3,10,11]
 field = ['T','V','U']
 for ppl in xplanes:
     dict_data[ppl] = dict()
     for ff in field
           dict_data[ppl][ff] = ...Some... numpy array


 
 

Now I write the dict to a csv file

  with open('file.csv','w') as f:
      write=csv.write(f)
      for k,v in dict_data.items():
           write.writerow([k,v])

The csv file has the following structure:

   7,"{'T': array([ 637525.25 ...... , ], dtype=float32)
    ,  'V': array([ 637525.25 ...... , ], dtype=float32)
    ,  'U': array([ 637525.25 ...... , ], dtype=float32)}"
   3,"{'T': array([ 637525.25 ...... , ], dtype=float32) ....
      "}

Now I am struggling to read that file and revert it back to the dict I used to have, namely, dict_data

CodePudding user response:

Read the csv file with read_csv from pandas. Then use to_dict() method.

import pandas as pd

data = pd.read_csv("file.csv")
print(data.to_dict())

CodePudding user response:

it did not go as smooth as i thought, so it's a bit ugly but should do

from numpy import array, float32
import re

txt="""
7,"{'T': array([1,2,3], dtype=float32)
,  'V': array([1,2,3], dtype=float32)
,  'U': array([1,2,3], dtype=float32)}"
3,"{'T': array([1,2,3], dtype=float32)
,  'V': array([1,2,3], dtype=float32)
,  'U': array([1,2,3], dtype=float32)}"
10,"{'T': array([1,2,3], dtype=float32)
,  'V': array([1,2,3], dtype=float32)
,  'U': array([1,2,3], dtype=float32)}"
11,"{'T': array([1,2,3], dtype=float32)
,  'V': array([1,2,3], dtype=float32)
,  'U': array([1,2,3], dtype=float32)}"
"""

splitter=re.compile('(^\d{1,2}),', re.MULTILINE)
get_k_v=re.compile("'([TUV])': (array[^)] \))")
splits=re.split(splitter, txt)
old_dict={}
for split in splits:
    if split=='\n':
        continue
    elif split.isdigit():
        k=int(split)
    else:
        res=re.findall(get_k_v, split)
        old_dict[k]={k: eval(v) for k,v in res}

print(old_dict[7]['T'])
>>> [1. 2. 3.]
  • Related