简体   繁体   中英

Python TypeError: '_csv.writer' object is not iterable

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" } } } }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM