Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy am I seeing "TypeError: string indices must be integers"?
    primarykey
    data
    text
    <p>I'm playing with both learning python and trying to get github issues into a readable form. Using the advice on <a href="https://stackoverflow.com/questions/1871524/convert-from-json-to-csv-using-python">How can I convert JSON to CSV?</a> I came up with this:</p> <pre><code>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"]]) </code></pre> <p>Where "issues.json" is the json file containing my github issues. When I try to run that, I get </p> <pre><code>File "foo.py", line 14, in &lt;module&gt; 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 </code></pre> <p>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!</p> <p><strong>UPDATE:</strong> When I tweak the <code>for</code> statement to simply </p> <pre><code>for item in data: print item </code></pre> <p>what I get is ... "issues" -- so I'm doing something more basic wrong. Here's a bit of my json:</p> <pre><code>{"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&gt;&gt;) links... </code></pre> <p>when I print <code>data</code> it looks like it is getting munged really oddly:</p> <pre><code>{u'issues': [{u'body': u'Add missing paging (Older&gt;&gt;) lin... </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload