[英]ascii codec error is showing while running python3 code via apche wsgi
Objective : Insert a Japanese text to a.ini file目标:将日语文本插入到 a.ini 文件中
Steps:脚步:
Issue:问题:
When I try running the code via the "flask run" command, there are no issues.当我尝试通过“flask run”命令运行代码时,没有问题。 The Japanese text is inserted to ini file correctly But when I try running the same code via apache(wsgi) I am getting the following error 'ascii' codec can't encode characters in position 17-23: ordinal not in range(128)
日文文本已正确插入到 ini 文件但是当我尝试通过 apache(wsgi) 运行相同的代码时,我收到以下错误 'ascii' codec can't encode characters in position 17-23: ordinal not in range(128)
Never interact with text files without explicitly specifying the encoding.切勿在未明确指定编码的情况下与文本文件交互。
Sadly, even Python's official documentation neglects to obey this simple rule.可悲的是,即使是 Python 的官方文档也忽略了遵守这个简单的规则。
import configparser
config_path = 'your_file.ini'
config = configparser.ConfigParser()
with open(config_path, encoding='utf8') as fp:
config.read_file(fp)
with open(config_path, 'w', encoding='utf8') as fp:
config.write(fp)
utf8
is a reasonable choice for storing Unicode characters, pick a different encoding if you have a preference. utf8
是存储 Unicode 字符的合理选择,如果您有偏好,请选择不同的编码。
Japanese characters consume up to five bytes per character in UTF-8, picking utf16
(always two bytes per character) can result in smaller ini
files, but there is no functional difference.日文字符在 UTF-8 中每个字符最多占用五个字节,选择
utf16
(每个字符总是两个字节)可以导致更小的ini
文件,但没有功能差异。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.