简体   繁体   English

错误:字符串索引必须是整数

[英]Error: String Indices must be integers

I am trying to convert a json string to a csv file. 我正在尝试将json字符串转换为csv文件。 I am getting the error:String Indices must be integers (All the nested keys are in the correct format). 我收到error:String Indices must be integers (所有嵌套键的格式均正确)。 Does the square bracket here is the reason for that? 请问这里的方括号是吗?

data":[\  

Following is the python file that i am trying to execute: 以下是我尝试执行的python文件:

import csv
import json

x="""{

"response":{"status":1,"httpStatus":200,"data":{"page":1,"current":50,"count":11012,"pageCount":221,

"data":[\
{"Offer":{"name":"abc","id":"884"},
"Affiliate":{"company":"POCKET","id":"1494"},
"Goal":{"name":null},
"Stat":{"year":"2015","month":"03","hour":"11","date":"2015-03-13","impressions":"0","gross_clicks":"1","unique_clicks":"1","clicks":"1","conversions":"0"}},

{"Offer":{"name":"qwerty","id":"884"},"Affiliate":{"company":"Rob","id":"1472"},"Goal":{"name":null},"Stat":{"year":"2015","month":"03","hour":"17","date":"2015-03-07","impressions":"0","gross_clicks":"2","unique_clicks":"2","clicks":"2","conversions":"0"}},


{"Offer":{"name":"1000","id":"2586"},"Affiliate":{"company":"Iron","id":"1156"},"Goal":{"name":null},"Stat":{"year":"2015","month":"07","hour":"13","date":"2015-07-07","impressions":"0","gross_clicks":"8628","unique_clicks":"8620","clicks":"8628","conversions":"6"}}],"dbSource":"brdb"},"errors":[],"errorMessage":null}}"""



x = json.loads(x)

f = csv.writer(open("S2.csv", "w",newline=""))
# Write CSV Header, If you dont need that, remove this line
f.writerow(["Offer", "Affiliate", "Goal", "year", "month","hour", "date", "impressions", "gross_clicks", "unique_clicks","clicks","conversions"])
print(x)
for x in x:
    f.writerow([x["response"]["data"]["data"]["Offer"]["name"], 
                x["response"]["data"]["data"]["Affiliate"]["company"], 
                x["response"]["data"]["data"]["Goal"]["name"], 
                x["response"]["data"]["data"]["Stat"]["year"],
                x["response"]["data"]["data"]["Stat"]["month"],
                x["response"]["data"]["data"]["Stat"]["hour"],
                x["response"]["data"]["data"]["Stat"]["date"],
                x["response"]["data"]["data"]["Stat"]["impressions"],
                x["response"]["data"]["data"]["Stat"]["gross_clicks"],
                x["response"]["data"]["data"]["Stat"]["unique_clicks"],
                x["response"]["data"]["data"]["Stat"]["clicks"],
                x["response"]["data"]["data"]["Stat"]["conversions"]
                ])


f.writerow([""])

x["response"]["data"]["data"] returns a list. x["response"]["data"]["data"]返回一个列表。

So x["response"]["data"]["data"]["Offer"] raises an error, as you are trying to access the key Offer in a list. 因此, x["response"]["data"]["data"]["Offer"]会引发错误,因为你正试图访问密钥Offer列表中。 Hence the error List Indices must be integers 因此,错误List Indices must be integers

Try this: 尝试这个:

x["response"]["data"]["data"][0]["Offer"]["name"]

Where x["response"]["data"]["data"][0] will access the dictionary in the within the list, from which you can access the keys "Offer" and "name" x["response"]["data"]["data"][0]将访问列表中的词典,从中可以访问键"Offer""name"

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

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