2
$\begingroup$

While converting json file to csv,I got the above error My code is shown below:

import json
import csv
data = open("SRInFlow.json",encoding="utf8")
rows = json.loads(data.read())
r=rows.values()
fieldnames = [b'dateTimeValues', b'timeSeries']
with open('C:/DeepLearning/tensorflowpoc/Codes/SRInFlow.csv', 'w') as f:
    dict_writer = csv.DictWriter(f, fieldnames=fieldnames)
    dict_writer.writeheader()
    for d in r:
  --->    dict_writer.writerow(d)

The error displays in the last line. How to rectify the error? Thanks in advance

$\endgroup$
4
  • $\begingroup$ It depends on your JSON structure $\endgroup$
    – Aditya
    Mar 9, 2018 at 12:24
  • $\begingroup$ Try printing r and type(r) to get an idea what you are dealing with. $\endgroup$ Mar 9, 2018 at 12:31
  • $\begingroup$ type(r) is <class 'dict_values'> $\endgroup$
    – Sheetal
    Mar 13, 2018 at 5:51
  • $\begingroup$ r = rows.values() ... for d in r: is a really bad code-smell that you're trying to iterate over just the row values, yet expect them to behave like a dict(!) Why not just simply iterate the normal way: for row in rows: $\endgroup$
    – smci
    May 24, 2019 at 23:15

1 Answer 1

3
$\begingroup$

This error is because you are initialising a dictionary writer dict_writer = csv.DictWriter(f, fieldnames=fieldnames) but then you are passing a normal row containing only the values r=rows.values() -> for d in r: ->dict_writer.writerow(d), when the writer expects a dictionary including the keys. That is what the error means.

I don't know how your JSON looks but you can try something like this:

import json
import csv

data = '{"dateTimeValues": [1,3,4], "timeSeries": [0,0,0]}'
rows = json.loads(data)

r=zip(*rows.values())

fieldnames = ['dateTimeValues', 'timeSeries']
with open('c:/Users/jsz.P3-GROUP/Desktop/shit.csv', 'w') as f:
    dict_writer = csv.writer(f)
    dict_writer.writerow(fieldnames)
    for d in r:
        dict_writer.writerow(d)

This initialises a simple csv writer dict_writer = csv.writer(f)(not dictionary), then inputs the fieldnames as a normal row for the header dict_writer.writerow(fieldnames) and finally inserts only the values as in your example.

Note that for my json string I had to transpose the values first as in r=zip(*rows.values()).

Hope this helps.

$\endgroup$
2
  • $\begingroup$ Thanks... but got an another error ''_csv.Error: iterable expected, not int ''. My json data is very large which contains 5-6 years of data and has two headings dateTimeValues and timeSeries $\endgroup$
    – Sheetal
    Mar 13, 2018 at 5:49
  • $\begingroup$ Have a look at this: stackoverflow.com/questions/33550125/… $\endgroup$
    – TitoOrt
    Mar 13, 2018 at 9:18

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.