Estoy jugando con Python y tratando de obtener problemas de github en una forma legible. Utilizando el asesoramiento sobre How can I convert JSON to CSV? me ocurrió esto:¿Por qué veo "TypeError: los índices de cadena deben ser enteros"?
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"]])
Donde "issues.json" es el archivo JSON que contiene mis problemas de GitHub. Cuando intento ejecutar eso, obtengo
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
¿Qué me falta aquí? ¿Cuáles son los "índices de cuerda"? Estoy seguro de que una vez que tenga esto en funcionamiento tendré más problemas, pero por ahora, ¡me encantaría que esto funcione!
ACTUALIZACIÓN: Cuando Tweak La declaración for
simplemente
for item in data:
print item
lo que se obtiene es ... "problemas" - por lo que estoy haciendo algo mal más básico. Aquí hay un poco de mi 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...
cuando imprimo data
parece que se está munged realmente extraño:
{u'issues': [{u'body': u'Add missing paging (Older>>) lin...
Lo que hace falta es 'print repr (data)' o 'import pprint; pprint.pprint (data) ' –
@IbrahimApachi Pregunta incorrecta. :) – Amanda