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

时间:2015-08-02 11:17:46

标签: python

我正在尝试将json字符串转换为csv文件。 我得到了error:String Indices must be integers (所有嵌套键的格式都正确)。 这里的方括号是什么原因?

data":[\  

以下是我尝试执行的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([""])

1 个答案:

答案 0 :(得分:0)

java.lang.RuntimeException: Unable to start activity ComponentInfo{example.home/example.home.AppsGridActivity}: java.lang.NullPointerException: Attempt to write to null array at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: java.lang.NullPointerException: Attempt to write to null array at example.home.AppsInfoModel.<init>(AppsInfoModel.java:25) at example.home.AppsGridActivity.onCreate(AppsGridActivity.java:21) at android.app.Activity.performCreate(Activity.java:5933) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)             at android.app.ActivityThread.access$800(ActivityThread.java:144)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)             at android.os.Handler.dispatchMessage(Handler.java:102)             at android.os.Looper.loop(Looper.java:135)             at android.app.ActivityThread.main(ActivityThread.java:5221)             at java.lang.reflect.Method.invoke(Native Method)             at java.lang.reflect.Method.invoke(Method.java:372)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 会返回一个列表。

因此,当您尝试访问列表中的密钥x["response"]["data"]["data"]时,x["response"]["data"]["data"]["Offer"]会引发错误。因此错误Offer

试试这个:

List Indices must be integers

x["response"]["data"]["data"][0]["Offer"]["name"] 将访问列表中的字典,您可以从中访问密钥x["response"]["data"]["data"][0]"Offer"