Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Facing this error “TypeError: string indices must be integers”?

I'm trying with both learning python and trying to get github issues into a readable form. Using the advice on How can I convert JSON to 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. 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!

When I tweak the for statement to simply

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:

{"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:

{u'issues': [{u'body': u'Add missing paging (Older>>) lin...
by

2 Answers

Bharatgxwzm
The variable item is a string. 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.

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
Shahlar1vxp
'Item' is a string in our code. The string indices are the characters inside the square brackets. First you must check your data variable to see what you receive.
For example, the following code shows an error-
>>> my_string = "hello world"
>>> my_string[0,5]

'TypeError:' string indices should be integers. We passed a tuple of two integers implicitly here, from 0 to 5 to the slice notation where we called 'my_string[0,5] as 0,5 evaluates to the same tuple as (0,5) does. We need to replace the comma with a colon in order to separate two integers correctly-
Like here, the code goes as-
>>> my_string = "hello world"
>>> my_string[0:5]
'hello'

Login / Signup to Answer the Question.