0

I have this data:

#my.csv
"accountid","configuration"
"797847293","{'dBInstanceIdentifier': 'grafanadb', 'dBInstanceClass': 'db.t3.micro', 'engine': 'postgres', 'dBInstanceStatus': 'available'}"

How can I bring into format like:

# result.csv
accountid,dBInstanceIdentifier,dbInstanceClass,engine
797847293,grafanadb,db.t3.micro,engine

I tried it with python pandas, but not result so far respectively errors.

Any ideas?

2
  • You will have to load the data and write an algorithm to transform the data as you wish, then write to the result.csv. Commented Apr 19, 2022 at 13:16
  • 1
    are you missing an extra " at the end of the second line? Commented Apr 19, 2022 at 13:22

2 Answers 2

1

You can read the csv to dataframe and convert configuration to dict with ast.literal_eval. Then use pd.json_normalize to convert a column of dictionary into columns.

import ast

df = pd.read_csv('data.csv', quotechar='"')

out = pd.concat([df['accountid'], pd.json_normalize(df['configuration'].apply(ast.literal_eval))], axis=1)
print(out)

   accountid dBInstanceIdentifier dBInstanceClass    engine dBInstanceStatus
0  797847293            grafanadb     db.t3.micro  postgres        available
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, that is what I was looking for. I could reduce my code a lot. Could be so easy to make the manager happy xD
1

Assuming you've gotten this csv file from some data source, it shouldn't be this inconsistent in its formatting and should have a double quote " at the end of the second line which I believe you've missed out when copying over. Like this:

#my.csv
"accountid","configuration"
"797847293","{'dBInstanceIdentifier': 'grafanadb', 'dBInstanceClass': 'db.t3.micro', 'engine': 'postgres', 'dBInstanceStatus': 'available'}"

If that is the case, you can simply specify the quotechar argument in pd.read_csv to tell it to ignore delimiters inside the double quotes "

df = pd.read_csv('a.csv', quotechar='"')

You can then do your processing in pandas before writing back into csv from here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.