相关疑难解决方法(0)

为什么我看到"TypeError:字符串索引必须是整数"?

我正在玩学习python并尝试将github问题变成可读的形式.使用有关如何将JSON转换为CSV的建议?我想出了这个:

import json
import csv

f=open('issues.json')
data = json.load(f)
f.close()

f=open("issues.csv","wb+")
csv_file=csv.writer(f)

csv_file.writerow(["gravatar_id","position","number","votes","created_at","comments","body","title","updated_at","html_url","user","labels","state"])

for item in data:
        csv_file.writerow([item["gravatar_id"], item["position"], item["number"], item["votes"], item["created_at"], item["comments"], item["body"], item["title"], item["updated_at"], item["html_url"], item["user"], item["labels"], item["state"]])
Run Code Online (Sandbox Code Playgroud)

其中"issues.json"是包含我的github问题的json文件.当我尝试运行时,我明白了

File "foo.py", line 14, in <module>
csv_file.writerow([item["gravatar_id"], item["position"], item["number"], item["votes"], item["created_at"], item["comments"], item["body"], item["title"], item["updated_at"], item["html_url"], item["user"], item["labels"], item["state"]])

TypeError: string indices must be integers
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么?哪个是"字符串索引"?我敢肯定,一旦我开始工作,我会有更多的问题,但就目前而言,我只是喜欢这个工作!

更新: 当我for简单地调整声明时

for item in data:
    print item
Run Code Online (Sandbox Code Playgroud)

我得到的是......"问题" - 所以我做了一些更基本的错误.这是我的一些json:

{"issues":[{"gravatar_id":"44230311a3dcd684b6c5f81bf2ec9f60","position":2.0,"number":263,"votes":0,"created_at":"2010/09/17 16:06:50 -0700","comments":11,"body":"Add missing paging (Older>>) links...
Run Code Online (Sandbox Code Playgroud)

当我打印 …

python json github

173
推荐指数
4
解决办法
66万
查看次数

标签 统计

github ×1

json ×1

python ×1