Preface: Take on a blog, help senior processing JSON file read into the database. Python reads data into the database, and a coding problem occurs. Python inside the definition of utf-8, however, the file has Japanese, Korean, other symbols, Latin code, and so on, not familiar with the code is dying silver, got a long time, but eventually a function can be broken. The Encode () function.
Decode () and encode () These two functions, when there is time or more to look at is.
#-*-Coding:utf-8-*-"" "Created on Fri Jul 16:49:56 2015@author:shifeng" "" Import Codecsimport jsonimport sysreload (s YS) sys.setdefaultencoding (' utf8 ') F = codecs.open ("Xxx.json") for line in F: line_dict = json.loads (line) Text = line_dict["text"] text = Text.encode (encoding= "Utf-8", errors = "ignores")The value of the text field in the Xxx.json file contains Chinese, Latin code, Japanese, Korean, and so on.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Python character encoding issues