I play with both learning python and trying to get github issues in a readable form. Using advice How to convert JSON to CSV? I came up with the following:
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 this, 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? What are string indexes? I am sure that as soon as I get this job, I will have more problems, but for now I just love to work!
UPDATE: When I configure the for statement simply
for item in data: print item
what I get ... "problems" - so I'm doing something more complex. Here is 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 looks very strange:
{u'issues': [{u'body': u'Add missing paging (Older>>) lin...