jeudi 22 mai 2014

Création d'un fichier json itérées en python ? -Débordement de pile


Currently I'm using:


import json
jsonlist = ["data", "test", "row", "blah", "boo"]

with open('test.txt', "wb") as jsfile:
jsfile.write(json.dumps(jsonlist))

My current output is:


["data", "test", "row", "blah", "boo"]

Idealing I'd like an iterated json file as seen below: (if that's correctly formated)


[1: "data",
2: "test",
3: "row",
4: "blah",
5: "boo"]

My actual data is rather large blocks of html, this is just an example of how I'm doing it...


Does anyone know how I can achieve this?




It's rarely worth thinking of how to convert one JSON representation into another; instead, convert one Python object into another, then represent that in JSON.


So, for example, given this Python list:


["data", "test", "row", "blah", "boo"]

… maybe you want this dict:


{1: "data", 2: "test", 3: "row", 4: "blah", 5: "boo"}

… which will JSONify to:


'{"1": "data", "2": "test", "3": "row", "4": "blah", "5": "boo"}'

Note that the keys have been turned into strings. The keys of JSON objects must be strings. If that's not what you want, you don't want JSON objects. Maybe you want a list of pairs in that case, which will turn into a JSON array of JSON arrays (since arrays can have numbers as values)?


enumerate can turn the list into a list of (index, value) pairs, and dict, can turn a list of (key, value) pairs into a dict:


jsonlist = ["data", "test", "row", "blah", "boo"]
jsondict = dict(enumerate(jsonlist, 1))
with open('test.txt', "wb") as jsfile:
jsfile.write(json.dumps(jsondict))

Or, if you just wanted an array of arrays, stop at the enumerate. Because enumerate actually gives you an iterator over the values, rather than a sequence, you'll have to wrap it in list:


jsonlist = ["data", "test", "row", "blah", "boo"]
jsonpairs = list(enumerate(jsonlist, 1))
with open('test.txt', "wb") as jsfile:
jsfile.write(json.dumps(jsonpairs))

Note that, because Python uses 0-based indexing, I had to pass a start argument to enumerate to count from 1 instead of 0.



Currently I'm using:


import json
jsonlist = ["data", "test", "row", "blah", "boo"]

with open('test.txt', "wb") as jsfile:
jsfile.write(json.dumps(jsonlist))

My current output is:


["data", "test", "row", "blah", "boo"]

Idealing I'd like an iterated json file as seen below: (if that's correctly formated)


[1: "data",
2: "test",
3: "row",
4: "blah",
5: "boo"]

My actual data is rather large blocks of html, this is just an example of how I'm doing it...


Does anyone know how I can achieve this?



It's rarely worth thinking of how to convert one JSON representation into another; instead, convert one Python object into another, then represent that in JSON.


So, for example, given this Python list:


["data", "test", "row", "blah", "boo"]

… maybe you want this dict:


{1: "data", 2: "test", 3: "row", 4: "blah", 5: "boo"}

… which will JSONify to:


'{"1": "data", "2": "test", "3": "row", "4": "blah", "5": "boo"}'

Note that the keys have been turned into strings. The keys of JSON objects must be strings. If that's not what you want, you don't want JSON objects. Maybe you want a list of pairs in that case, which will turn into a JSON array of JSON arrays (since arrays can have numbers as values)?


enumerate can turn the list into a list of (index, value) pairs, and dict, can turn a list of (key, value) pairs into a dict:


jsonlist = ["data", "test", "row", "blah", "boo"]
jsondict = dict(enumerate(jsonlist, 1))
with open('test.txt', "wb") as jsfile:
jsfile.write(json.dumps(jsondict))

Or, if you just wanted an array of arrays, stop at the enumerate. Because enumerate actually gives you an iterator over the values, rather than a sequence, you'll have to wrap it in list:


jsonlist = ["data", "test", "row", "blah", "boo"]
jsonpairs = list(enumerate(jsonlist, 1))
with open('test.txt', "wb") as jsfile:
jsfile.write(json.dumps(jsonpairs))

Note that, because Python uses 0-based indexing, I had to pass a start argument to enumerate to count from 1 instead of 0.


0 commentaires:

Enregistrer un commentaire