About json.dumps of parameters, such as ensure_ascii

By help ( "json") see there are a configuration information

<span style = "font-Family: the Microsoft YaHei; font-size: 18px;">
dumps (obj, skipkeys = False, ensure_ascii = True, check_circular = True, allow_nan = True, CLS = None, indent = None, Separators = none, default = none, sort_keys = False, ** kW)
</ span>
compact format json encoded output string, and there is no order, so dumps method provides some optional parameters, so that to improve the output format readability.
(1) sort_keys according to tells the encoder dictionary sorting (a to z) output.

import json

data = [ { 'a':'A', 'b':(2, 4), 'c':3.0 } ]
print 'DATA:', repr(data)

unsorted = json.dumps(data)
print 'JSON:', json.dumps(data)
print 'SORT:', json.dumps(data, sort_keys=True)
输出:

The DATA: [{ 'A': 'A', 'C': 3.0, 'B': (2,. 4)}]
the JSON: [{ "A": "A", "C": 3.0, "B" : [2,. 4]}]
the SORT: [{ "A": "A", "B": [2,. 4], "C": 3.0}

(2) indent indented format parameters according to the data, read Clearer:

indent value, representing the number of bits indent

import json

data = [ { 'a':'A', 'b':(2, 4), 'c':3.0 } ]
print 'DATA:', repr(data)

print 'NORMAL:', json.dumps(data, sort_keys=True)
print 'INDENT:', json.dumps(data, sort_keys=True, indent=2)
输出:

The DATA: [{ 'A': 'A', 'C': 3.0, 'B': (2,. 4)}]
the NORMAL: [{ "A": "A", "B": [2,. 4] , "C": 3.0}]
INDENT: [
{
"a": "a",
"B": [
2,
. 4
],
"C": 3.0
}
]
action (3) separators are removed ,, parameters: rear spaces, the output from the above results can be seen ":" followed by a space there, which is to beautify the output effect, but in the process we transfer data, the better the more streamlined, redundant stuff all removed, so you can add.

data = [ { 'a':'A', 'b':(2, 4), 'c':3.0 } ]
print 'DATA:', repr(data)
print 'repr(data) :', len(repr(data))
print 'dumps(data) :', len(json.dumps(data))
print 'dumps(data, indent=2) :', len(json.dumps(data, indent=2))
print 'dumps(data, separators):', len(json.dumps(data, separators=(',',':')))
print json.dumps(data, sort_keys=True)
print json.dumps(data, sort_keys=True, separators=(',',':'))

输出结果为:

The DATA: [{ 'A': 'A', 'C': 3.0, 'B': (2,. 4)}]
the repr (Data): 35
dumps (Data): 35
dumps (Data, indent = 2): 76
dumps (Data, Separators): 29
[{ "A": "A", "B": [2,. 4], "C": 3.0}]
[{ "A": "A", "B": [2,4], "C": 3.0}]

(. 4) skipkeys parameters in the encoding process, key dict string object may be an object only if the other types, it will be thrown in the encoding process of ValueError abnormal. skipkeys can skip those non-string object as the processing key.

data2 = [ { 'a':'A', 'b':(2, 4), 'c':3.0, ('d',):'D tuple' } ]
try:
print json.dumps(data2)
except (TypeError, ValueError) as err:
print 'ERROR:', err
print
print json.dumps(data2, skipkeys=True)

>> output is:
> ERROR: Keys MUST BE A String

[{ "A": "A", "C": 3.0, "B": [2,. 4]}]

(. 5) outputs a real need to specify Chinese ensure_ascii = False

If no configuration, or use the default configuration,
the output would be 'cool' ASCII characters do, rather than a real Chinese.
This is because Chinese ascii default encoding used when json.dumps serialization.

import json

Print json.dumps ( "cool")

>> "\ u51c9 \ u51c9"

want to output the real Chinese need to specify ensure_ascii = False:

print json.dumps ( 'cool', ensure_ascii = False)
>> cool


description link: https: //blog.csdn.net/liangxy2014/article/details/78985057

Guess you like

Origin www.cnblogs.com/fcc-123/p/12294353.html