Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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... 
like image 867
Amanda Avatar asked May 20 '11 21:05

Amanda


People also ask

How do I fix error string indices must be integers?

If you encounter this error message, double check to make sure you are using the numerical index value to access elements instead of a string value.

Do string indices have to be integers?

String indices must be integers. This means that when you're accessing an iterable object like a string, you must do it using a numerical value. If you are accessing items from a dictionary, make sure that you are accessing the dictionary itself and not a key in the dictionary.

How do you fix a string index out of range in Python?

In Python console, create a string variable and type of the variable. Now, print each character of the string using its index. If you try to access the extra characters from the string, you will get the IndexError: string index out of range.

How do you convert a string to an integer in Python?

To convert, or cast, a string to an integer in Python, you use the int() built-in function. The function takes in as a parameter the initial string you want to convert, and returns the integer equivalent of the value you passed. The general syntax looks something like this: int("str") .


2 Answers

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 
like image 180
bluepnume Avatar answered Oct 29 '22 07:10

bluepnume


item is most likely a string in your code; the string indices are the ones in the square brackets, e.g., gravatar_id. So I'd first check your data variable to see what you received there; I guess that data is a list of strings (or at least a list containing at least one string) while it should be a list of dictionaries.

like image 21
Tamás Avatar answered Oct 29 '22 07:10

Tamás