Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
616 views
in Technique[技术] by (71.8m points)

python - 为什么会看到“ TypeError:字符串索引必须为整数”?(Why am I seeing “TypeError: string indices must be integers”?)

I'm playing with both learning python and trying to get github issues into a readable form.

(我正在学习python并试图将github问题转换为可读形式。)

Using the advice on How can I convert JSON to CSV?

(使用有关如何将JSON转换为CSV的建议?)

I came up with this:

(我想出了这个:)

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"]])

Where "issues.json" is the json file containing my github issues.

(其中“ issues.json”是包含我的github问题的json文件。)

When I try to run that, I get

(当我尝试运行它时,我得到)

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

What am I missing here?

(我在这里想念什么?)

Which are the "string indices"?

(哪些是“字符串索引”?)

I'm sure that once I get this working I'll have more issues, but for now , I'd just love for this to work!

(我确定一旦完成这项工作,我就会遇到更多问题,但是就目前而言,我只是希望它能正常工作!)

UPDATE: When I tweak the for statement to simply

(更新:当我调整for语句以简单地)

for item in data:
    print item

what I get is ... "issues" -- so I'm doing something more basic wrong.

(我得到的是...“问题”-所以我在做一些更基本的错误。)

Here's a bit of my json:

(这是我的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...

when I print data it looks like it is getting munged really oddly:

(当我打印data ,看起来好像真的很奇怪:)

{u'issues': [{u'body': u'Add missing paging (Older>>) lin...
  ask by Amanda translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The variable item is a string.

(可变item是一个字符串。)

An index looks like this:

(索引如下所示:)

>>> mystring = 'helloworld'
>>> print mystring[0]
'h'

The above example uses the 0 index of the string to refer to the first character.

(上面的示例使用字符串的0索引来引用第一个字符。)

Strings can't have string indices (like dictionaries can).

(字符串不能具有字符串索引(就像字典一样)。)

So this won't work:

(所以这行不通:)

>>> mystring = 'helloworld'
>>> print mystring['stringindex']
TypeError: string indices must be integers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...