[英]Python UnicodeEncodeError: 'ascii' codec can't encode characters
There is a json data which contains some Chinese characters. 有一个包含一些中文字符的json数据。
{
"font_size": "47",
"sentences": [
"你好",
"sample sentence1",
"sample sentence2",
"sample sentence3",
"sample sentence4",
"sample sentence5",
"sample sentence6",
"sample sentence7",
"sample sentence8",
"sample sentence9"
]
}
I create a Flask
app and use it to receive above json data. 我创建了一个Flask
应用程序并使用它来接收上面的json数据。 I use below curl command to post data. 我使用下面的curl命令发布数据。
curl -X POST \
http://0.0.0.0:5000/ \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json;charset=UTF-8' \
-H 'Postman-Token: af380f7a-42a8-cfbb-9177-74bb348ce5ed' \
-d '{
"font_size": "47",
"sentences": [
"你好",
"sample sentence1",
"sample sentence2",
"sample sentence3",
"sample sentence4",
"sample sentence5",
"sample sentence6",
"sample sentence7",
"sample sentence8",
"sample sentence9"
]
}'
After I receive json data from request.data
, I convert it to json, in fact request.data
is str
. 从request.data
收到json数据后,我将其转换为json,实际上request.data
是str
。
json_data = json.loads(request.data)
Then I want to format a string with json_data
. 然后我想用json_data
格式化一个字符串。
subtitles.format(**json_data)
I got an error. 我收到了一个错误。
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128) UnicodeEncodeError:'ascii'编解码器无法编码位置0-1中的字符:序数不在范围内(128)
How to solve it? 怎么解决? Thanks in advance. 提前致谢。
subtitles
is read from file. 从文件中读取subtitles
。
subtitles_file = "{path}/templates/sorry/subtitles.ass".format(path=root_path)
with open(subtitles_file, 'r') as file:
subtitles = file.read()
I'm using python 2 and this error occurs. 我正在使用python 2并发生此错误。 However Python 3 can automatically handle this. 但是Python 3可以自动处理这个问题。 So enjoy Python 3. 所以享受Python 3。
In Python 2, when you open
and read
a file, what you get is a regular str
, not a unicode
. 在Python 2中,当您open
并read
文件时,您获得的是常规str
,而不是unicode
。
Meanwhile, even if request.data
is a str
rather than a unicode
, if any of the strings in it are non-ASCII, json_data
will contain unicode
. 同时,即使request.data
是str
而不是unicode
,如果其中的任何字符串都是非ASCII, json_data
将包含unicode
。
So, when you do subtitles.format
, that's going to try to encode
each unicode
using your default encoding—which, if you haven't done anything, is ASCII. 所以,当你做subtitles.format
,那将尝试使用你的默认编码encode
每个unicode
进行编码 - 如果你还没有做任何事情,那就是ASCII。 Which will give exactly this error. 哪个会给出这个错误。
The simplest fix is to change subtitles
to a unicode
. 最简单的解决方法是将subtitles
更改为unicode
。 Like this: 像这样:
with open(subtitles_file, 'r') as file:
subtitles = file.read().decode('utf-8')
… or: … 要么:
with codecs.open(subtitles_file, 'r', 'utf-8') as file:
subtitles = file.read()
(I'm guessing that you want UTF-8; if your files are in some other encoding, obviously use that instead.) (我猜你想要UTF-8;如果你的文件是其他编码,显然要使用它。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.