0

I am parsing json to csv. But am getting error as below:

for i in data:
TypeError: '_csv.writer' object is not iterable

Code:

import json
import csv

with open("Data.json", 'r') as file:
    data = json.load(file)

CSV_File = 'Data.csv'

with open(CSV_File, 'w') as file:
    data = csv.writer(file)
    data.writerow([])
for i in data:
    data.writerow([])

Data

{ "id": "kljhfksdhkhd", "name": "BOB", "birthday": "08/03/1993", "languages": [ { "id": "106059522759137", "name": "English language" }, { "id": "107617475934611", "name": "Telugu language" }, { "id": "112969428713061", "name": "Hindi" }, { "id": "343306413260", "name": "Tamil language" }, { "id": "100904156616786", "name": "Kannada" } ], "games": { "data": [ { "name": "Modern Combat", "id": "12134323", "created_time": "2019-02-21T18:39:41+0000" }, { "name": "Cards", "id": "343232", "created_time": "2011-06-01T11:13:31+0000" }, { "name": "Shuttle Badminton", "id": "43214321", "created_time": "2011-06-01T11:13:31+0000" }, { "name": "Carrom", "id": "49y497", "created_time": "2011-06-01T11:13:31+0000" }, { "name": "Chess", "id": "0984080830", "created_time": "2011-06-01T11:13:31+0000" } ], "paging": { "cursors": { "before": "dkkskd", "after": "dlldlkd" } } } }

6
  • The error message suggests the json file is empty. Also, not sure naming all the variables data is going to be helpful. Commented May 2, 2020 at 13:53
  • No the file has data, its not empty Commented May 2, 2020 at 13:58
  • Is it json? Can you share a sample? Commented May 2, 2020 at 14:00
  • My bad, the file is empty. Now any getting another error. Could you help me, Updated he error Commented May 2, 2020 at 14:08
  • 1
    "not sure naming all the variables data is going to be helpful" Commented May 2, 2020 at 14:10

1 Answer 1

2

First off, the name data has been assigned to two different objects. Python permits this each assignment overwrites the previous. In the code, data is initially the data from the json file, then a csv.writer instance. A sensible improvement, therefore, is to name the writer writer, and change the code accordingly:

import json
import csv

with open("Data.json", 'r') as file:
    data = json.load(file)

CSV_File = 'Data.csv'

with open(CSV_File, 'w') as file:
    writer = csv.writer(file)
    writer.writerow([])
for i in data:
    writer.writerow([])

Now let's deal with how we are writing to the file. writer.writerow expects a list, but writing an empty list: writer.writerow([]) isn't very useful. Probably you want to write the json data to the csv file, so leet's get rid of the empty lists, and indent the writing loop so that it's inside the with block (otherwise the file will be closed).

import json
import csv

with open("Data.json", 'r') as file:
    data = json.load(file)

CSV_File = 'Data.csv'

with open(CSV_File, 'w') as file:
    writer = csv.writer(file)
    for row in data:
        writer.writerow(row)

This will work if the json data is a list of lists, that is it looks like this:

[[...], [...], [...], ...]

because each element of the outer list is a list, so iterating over it (for row in data:) yields a list, which writer.writerow can handle. However it's not uncommon for json data to be in the form of a dictionary:

{"k1": [....], "k2": [...], "k3": [...], ...}

In this case, you might want to iterate over the dictionary's values, if they are list:

for row in data.values():
    writer.writerow(row)

Finally, the json may be an irregular mix of lists and dictionaries, and may be arbitrarily nested. It's up to you to determine how to map nested json data to the flat csv format.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your quick review. But its not helping out. Attaching my data, please review and help out

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.