简体   繁体   中英

How to modify a JSON file using Python without losing comments in the file

I have a settings.json file full with useful comments (sometimes C-style and sometimes Python), and I'm programmatically modifying them with eg json library, but when I save the modified one I lose all the comments explaining the fields. Another inconvenience is losing the indentations and spacing therein.

Is there a 'neat' way of modifying the file programmatically?

Standard json files cannot possibly have comments and still be compliant json.

There is another format that was designed to overcome this problem: json5 . It has libraries designed to keep json5 properties like comments intact - you can python library for it here .

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