简体   繁体   English

Python TypeError: '_csv.writer' object 不可迭代

[英]Python TypeError: '_csv.writer' object is not iterable

I am parsing json to csv.我正在将 json 解析为 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" } } } } {“id”:“kljhfksdhkhd”,“名称”:“BOB”,“生日”:“08/03/1993”,“语言”:[ {“id”:“106059522759137”,“名称”:“英语" }, { "id": "107617475934611", "name": "泰卢固语" }, { "id": "112969428713061", "name": "印地语" }, { "id": "343306413260", "名称”:“泰米尔语”},{“id”:“100904156616786”,“名称”:“卡纳达语”}],“游戏”:{“数据”:[{“名称”:“现代战斗”,“id ": "12134323", "created_time": "2019-02-21T18:39:41+0000" }, { "name": "卡片", "id": "343232", "created_time": "2011-06 -01T11:13:31+0000" }, { "name": "穿梭羽毛球", "id": "43214321", "created_time": "2011-06-01T11:13:31+0000" }, {"名称”:“卡罗姆”,“id”:“49y497”,“created_time”:“2011-06-01T11:13:31+0000”},{“名称”:“国际象棋”,“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.首先,名称data已分配给两个不同的对象。 Python permits this each assignment overwrites the previous. Python 允许此每个分配覆盖前一个。 In the code, data is initially the data from the json file, then a csv.writer instance.在代码中, data最初是来自 json 文件的数据,然后是csv.writer实例。 A sensible improvement, therefore, is to name the writer writer , and change the code accordingly:因此,一个明智的改进是将作者命名为writer并相应地更改代码:

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. writer.writerow需要一个列表,但写一个空列表: writer.writerow([])不是很有用。 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).可能你想将 json 数据写入 csv 文件,所以 leet 去掉空列表,缩进写入循环,使其位于with块内(否则文件将被关闭)。

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:如果 json 数据是列表列表,这将起作用,它看起来像这样:

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

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.因为外部列表的每个元素都是一个列表,所以对其进行迭代( for row in data: :)会产生一个列表, writer.writerow可以处理该列表。 However it's not uncommon for json data to be in the form of a dictionary:然而,json 数据采用字典形式的情况并不少见:

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

In this case, you might want to iterate over the dictionary's values , if they are list:在这种情况下,您可能希望遍历字典的values ,如果它们是列表:

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.最后,json 可能是列表和字典的不规则混合,可以任意嵌套。 It's up to you to determine how to map nested json data to the flat csv format.由您决定如何将 map 嵌套的 json 数据转换为平面 csv 格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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