Python outputs a string of Chinese characters that can be displayed correctly on the console (using UTF-8 encoding on the console) through print, but after writing to the file, the Chinese characters are printed in ASCII encoding. The English character can display readable characters normally.
Cause: Json.dumps The ASCII encoding that is used by default when serializing, want to output true Chinese need to specify ensure_ascii=false: more in-depth analysis, is to dJSON object is not purely Unicode implementation, Instead, it contains mixed Unicode encodings and strings that have been encoded with utf-8.
The possible ways are as follows:
1 ImportOS2 ImportOs.path3 Importio4 ImportSYS5 ImportJSON6 7Contentbypage = {}8contentbypage['Document'] =ShortName9contentbypage['content'] =textTencontentbypage['pagenumber'] =pagenumber OneJsonfile = Io.open (shortname+"--"+str (pagenumber) +". JSON",'W', encoding='UTF8') AJsonfile.write (Json.dumps (Contentbypage, Ensure_ascii=false). Decode ('UTF8')) - Jsonfile.flush () -Jsonfile.close ()
Python json.dumps () Chinese garbled problem