简体   繁体   中英

How to create newline delimited json-lines file from list of python dictionaries

I am trying to create a JSON-lines file of data so that is compatible with google cloud AI platform's requirements for online prediction .

Right now I have a list of dictionaries for each of my data points. It looks like this:

data = [{'values': [0,1,0], 'key': 0}, {'values': [1,1,0], 'key': 1}]

I'm exporting this data to data.json with the following code:

import json
json_filepath = "data.json"
with open(json_filepath, 'w') as f:
    json.dump(data, f)

The problem is, this data.json file then looks exactly like my data (viz. a list of dictionaries). How can I make this data.json file a new-line delimited collection of each dictionary in the list? In other words, how can I make it look like this:

{'values': [0,1,0], 'key': 0}
{'values': [1,1,0], 'key': 1}

You can loop through the array and dump each object followed by a new line '\\n' :

with open(json_filepath, 'w') as f:
    for d in data:
        json.dump(d, f)
        f.write('\n')

Alternatively, you can use a one-liner using json.dumps , str.join , and map :

with open(json_filepath, 'w') as f:
    f.write('\n'.join(map(json.dumps, data)))
with open(json_filepath, "w") as f:
    for datum in data:
        f.write(json.dumps(datum))
        f.write("\n")

I should also mention in general this is known as the "JSON Lines" data format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM