Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to explicitly set carriage return when doing json.dump?

My python script generates json file. And I have to support this python file working on windows and linux. The problem is a difference carriage return on windows and linux. When I run this code on windows, it outputs CRLF json. And it outputs LF json when I run this on linux.

So how to explicitly set carriage return when doing json dump in python3.5? I couln

import json
fpath = "hoge.json"
data = {"AGE": 12, "HOGE": [{"GUA": 3}]}
with open(fpath, 'wt', encoding="utf-8") as outfile:
    json.dump(data, outfile, indent=4, sort_keys=True, ensure_ascii=False)

http://docs.python.jp/3/library/json.html

like image 632
jef Avatar asked Jan 25 '17 03:01

jef


People also ask

What is the return type of JSON dumps?

dumps() takes in a json object and returns a string.

What is default in JSON dump?

The default is (', ', ': ') if indent is None and (', ', ': ') otherwise. To get the most compact JSON representation, you should specify (', ', ':') to eliminate whitespace. default: If specified, default should be a function that gets called for objects that can't otherwise be serialized.

What is JSON dumps () method?

The dump() method is used when the Python objects have to be stored in a file. The dumps() is used when the objects are required to be in string format and is used for parsing, printing, etc, . The dump() needs the json file name in which the output has to be stored as an argument.

What does JSON loads () return?

load or json. loads() method, it returns a Python dictionary. If you want to convert JSON into a custom Python object then we can write a custom JSON decoder and pass it to the json. loads() method so we can get a custom Class object instead of a dictionary.


2 Answers

If you insist on consistent CRLF behavior (the JSON spec requires parsers to handle both, but opening it in certain plain text readers like Notepad might be easier with consistent CRLF), the solution is in the open function, not the json module.

Just pass newline='\r\n' to open, and it will translate any \n written by json to \r\n seamlessly on all systems, rather than the default behavior of translating to os.linesep (which is \r\n on Windows and \n on most other OSes):

with open(fpath, 'w', encoding="utf-8", newline='\r\n') as outfile:
    json.dump(data, outfile, indent=4, sort_keys=True, ensure_ascii=False)
like image 84
ShadowRanger Avatar answered Sep 24 '22 02:09

ShadowRanger


line endings are white space in the json spec (https://www.rfc-editor.org/rfc/rfc7159 section 2 at the bottom):

Insignificant whitespace is allowed before or after any of the six structural characters.

  ws = *(
          %x20 /              ; Space
          %x09 /              ; Horizontal tab
          %x0A /              ; Line feed or New line
          %x0D )              ; Carriage return

meaning that every reader must handle both CR and LF to be conformant.

like image 34
thebjorn Avatar answered Sep 24 '22 02:09

thebjorn