last day (15 days later) » 

10:42 PM
0
A: convert json to csv using python pandas

perlimport pandas as pd import json with open('test.json') as f: content = f.read().replace('U',',')[::-1].replace(',', '', 1)[::-1] content = '[{}]'.format(content) l = json.loads(content) d = [{k1+k2: v2 for k2,v2 in v1.items()} for x in l for k1,v1 in x.items()] df = pd.DataFrame(d2, co...

 
Str object has no attribute items error
 
How does your d variable look like? This works on a dict where keys are strings and values are dicts (string => string)
 
D variable is reading file data
Some values are strings and some are integer and some are booleans
 
Updated my answer, you can read the file to s and then set d = json.loads(s) (requires import json)
 
Python TypeError: expected string or buffer on Json.load
Actually while reading file I am replacing unwanted characters because the file is not well structured json file
 
10:42 PM
I've updated the answer to read JSON from the file
And an alternative is to read the file (fixing the problems with that JSON) into a string s, and then set d = json.loads(s)
 
i have updated my question with how i am reading json file and replacing a string
 
OK, so you can get that code executed without an error, right? What happens when you execute pd.DataFrame([{k1+k2:v2 for k2,v2 in v1.items()} for k1,v1 in d.items()])?
 
i am getting error on json.loads ie.Python TypeError: expected string or buffer on Json.load
 
Hm, I've updated my code to match yours (I fixed the replce typo though), and I don't have any problem running it... can you try running print(type(content)) after reading from the file please?
 
Your code is working but the header is getting sorted.I need how it comes from input.How i shown in output.
 
10:42 PM
Please see the updated answer
 
getting l is undefined in [{k1+k2: v2 for k2,v2 in v1.items()} for x in l for k1,v1 in x.items()]
 
Are you sure you are using my code above? There are some important changes to how the file is processed (since your updated structure is different from the one that you posted yesterday)
 
unicode object has no attribute items error for x.items from l.
if am using the same code i am getting NO JSON OBJECT could be decoded.
 
And your input file is exactly the same as you posted?
I've updated the code a bit to make it more robust, please try once again
 
ya i solved it but headers are not getting what i have given in the output.
 
10:42 PM
There was some problem with my previous update, see updated code and the exact output of that code, which matches your expected output
 
Okay.But on using your logic headers are repeating for each line the headers are unique on my input.
 
Isn't my output exactly the same as the expected output in your question (under "output csv file should be")? I can't see any difference
 
I have completed one thing i have to do is can i get the one object in one line in csv file.
can you see my updated output.need one object in one row.
 
Hi! Sorry was busy with something else, I saw your new question and posted the answer there
 

last day (15 days later) »