The coding problem of Python3 has been relatively simple
In-memory strings are Unicode
Save to file with Utf-8
The following are the processes that str,byte convert to each other:
str = "ABC Learning "
Str
OUT[6]: ' ABC Learning '
MyByte = str.encode("utf-8")
MyByte
OUT[8]: B ' abc\xe5\xad\xa6\xe4\xb9\xa0 '
STR2 = MyByte. Decode ("utf-8")
str2
OUT[10]: ' ABC Learning '
Recently, when writing JSON-related file access, you encounter such a problem:
Import JSON
Json_str = "" "{"a":" 1","F":"100\n","b":" Study Hard "}" ""
Json_str
OUT[20]: ' {"a": " 1", "F": "100\n", "b": " Study Hard "} '
Json_str = json_str.encode(' Unicode_escape '). Decode (' Utf-8 ')
Json_str
OUT[22]: ' {'a': ' 1', 'f': '100\\n', 'b': '\\u597d\\u597d\\u5b66\\ U4e60"}"
Json_data = json. loads (Json_str, encoding= "utf-8")
Json_data
OUT[24]: {' A ': ' 1 ', ' B ': ' Study hard ', ' f ': ' 100\n '}
DUMPS_STR = json. dumps (Json_data, indent=4)
Dumps_str
OUT[26]: ' {\ n 'f': '100\\n', \ n 'a': ' 1', \ n 'b': '\ \ U597d\\u597d\\u5b66\\u4e60"\ n}"
Json_data = json. loads (DUMPS_STR)
Json_data
OUT[28]: {' A ': ' 1 ', ' B ': ' Study hard ', ' f ': ' 100\n '}
From the above example, through a string into JSON, and then through the json.dumps into a string, the original good learning into the Chinese code, stored in the file is also Chinese encoding, very not intuitive.
After groping, use the following method to make dumps into Chinese (ensure_ascii=false)
DUMPS_STR = json. dumps (Json_data, Ensure_ascii=false, indent=4)
Dumps_str
OUT[30]: ' {\ n 'f': '100\\n', \ n "a": " 1", \ n "b": " study hard. ' \ n} '
Python3 processing JSON file containing Chinese dumps