Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop python writing json file to one line

Tags:

python

json

How can I force Python to stop writing my .json file to one line? I've tried changing the write command with open('manifest.json', 'wb') as f: to with open('manifest.json', 'w') as f: but it is still writing to one line.

Code

import json

with open('manifest.json', 'r') as f:
    json_data = json.load(f)
    json_data['server-version'] = "xxxx-x.x.x"

with open('manifest.json', 'w') as f:
    f.write(json.dumps(json_data))

print('Successful.')
like image 212
brownzilla Avatar asked Aug 21 '15 00:08

brownzilla


1 Answers

For pretty printing use indent parameter

print(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4, separators=(',', ': ')))
like image 78
Maksym Kozlenko Avatar answered Sep 22 '22 09:09

Maksym Kozlenko