简体   繁体   English

Python UnicodeEncodeError:'ascii'编解码器无法编码字符

[英]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.datastr

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. 提前致谢。

EDIT 编辑

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()

EDIT Python 2 or Python 3 编辑Python 2或Python 3

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中,当您openread文件时,您获得的是常规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.datastr而不是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.

相关问题 UnicodeEncodeError:'ascii'编解码器无法编码字符 - UnicodeEncodeError: 'ascii' codec can't encode characters UnicodeEncodeError:“ ascii”编解码器无法对不在范围内的字符进行编码(128) - UnicodeEncodeError: 'ascii' codec can't encode characters ordinal not in range(128) Python mmh3:UnicodeEncodeError:'ascii'编解码器无法在位置0-14处编码字符:序数不在范围内(128) - Python mmh3: UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-14: ordinal not in range(128) Python:UnicodeEncodeError:'ascii'编解码器无法在位置34-39处编码字符:序数不在范围内(128) - Python: UnicodeEncodeError: 'ascii' codec can't encode characters in position 34-39: ordinal not in range(128) Python2.7 UnicodeEncodeError:'ascii'编解码器不能编码0-11位的字符:序号不在范围内(128) - Python2.7 UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-11: ordinal not in range(128) Python2.7 UnicodeEncodeError:“ascii”编解码器无法编码 dataframe 值中的字符 - Python2.7 UnicodeEncodeError: 'ascii' codec can't encode characters in dataframe values 在 python 中,UnicodeEncodeError:'ascii' 编解码器无法对 position 中的字符进行编码 15-18:最终不在范围内(128) - in python, UnicodeEncodeError: 'ascii' codec can't encode characters in position 15-18: inal not in range(128) Python3中的“ UnicodeEncodeError:'ascii'编解码器无法编码字符” - “UnicodeEncodeError: 'ascii' codec can't encode character” in Python3 收到UnicodeEncodeError的Python脚本:“ ascii”编解码器无法编码字符 - Python script receiving a UnicodeEncodeError: 'ascii' codec can't encode character Python错误:UnicodeEncodeError:'ascii'编解码器无法编码字符 - Python error : UnicodeEncodeError: 'ascii' codec can't encode character
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM