2

I have a list of dicts j and I would like to export some of the dicts as a json file called myFile.json :

for item in j:
    if item['start'] = "True":
        #If myFile.json exists then append item to myFile.json
        #Otherwise create myFile.json that starts with "[" and append item to it
#Append "]" to the myFile.json

I can do it using try , but I would like to know if there is a more pythonic way to make it .

My code doesn't even worth to put it.

try:
    with io.open(myFile.json, 'a', encoding='utf-8') as f:
        f.write(unicode(json.dumps(item, ensure_ascii=False)))
        f.write(u",")
except IOError:
    with io.open(myFile.json, 'w', encoding='utf-8') as f:
        f.write(u"[")
    with io.open(myFile.json, 'a', encoding='utf-8') as f:
        f.write(unicode(json.dumps(item, ensure_ascii=False)))
        f.write(u",")

        # ..etc

Edit The output file should be a json array:

[ {"key1":"value1","key2":"value2"},{"key1":"value3","key2":"value4"}]
4m1nh4j1
  • 4,289
  • 16
  • 62
  • 104
  • I don't understand how you can have valid JSON in your file if you can append data to it. Also, you need to fix your indentation. Presumably, the exception-catch should be at the same level as the try. – hughdbrown Feb 13 '14 at 16:25
  • This code doesn't run, right? `if item['start'] = "True":` You want something more like: `if item['start']` where `item['start']` is a boolean, not a string. – hughdbrown Feb 13 '14 at 16:27
  • @hughdbrown I don't have a valid json file but I would like to create it . The code run but without "True" in it, what I wrote is just an example.. – 4m1nh4j1 Feb 13 '14 at 16:35
  • I'm confused as to why you can't combine them all into a list before writing (should only be doing file i/o at the end of processing). I'm also confused as to why you couldn't open the file, read in the data, do a json.loads() and then append things to the list before rewriting it out. I tried doing what you're trying to do and it's a freaking headache, and usually you'll miss something on the formatting and be stuck with almost well formatted JSON – Jeff Langemeier Feb 13 '14 at 17:22

1 Answers1

4

Your approach has a severe flaw: If you are writing [ first, you also need to add , commas after each JSON value you write, you have to append the closing ], and then you have to remove the last ] every time you append to the file, or on reading manually add the ] closing bracket before decoding.

You'd be better of not trying to build a big JSON list, and instead use newlines as delimiters. That way you can freely append, and by reading your file line-by-line you can easily load the data again.

This has the added advantage of vastly simplifying your code:

with io.open(myFile.json, 'a', encoding='utf-8') as f:
    f.write(unicode(json.dumps(item, ensure_ascii=False)))
    f.write(u'\n')

This removes the need to test for the file existing first. Reading is as simple as:

with open(myFile.json) as f:
    for line in f:
        data = json.loads(line)
Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thank you @Martijn the second code is good for me, but why it doesn't create file if it doesn't exist, and doesn't work if I added this `open(myfile, 'w').close()` before ? – 4m1nh4j1 Feb 13 '14 at 18:20
  • @4m1nh4j1: opening a file in append mode creates the file for you if it doesn't exist. – Martijn Pieters Feb 13 '14 at 18:23