简体   繁体   English

通过 apche wsgi 运行 python3 代码时显示 ascii 编解码器错误

[英]ascii codec error is showing while running python3 code via apche wsgi

Objective : Insert a Japanese text to a.ini file目标:将日语文本插入到 a.ini 文件中

Steps:脚步:

  1. Python version used 3.6 and used Flask framework Python版本使用3.6和使用Flask框架
  2. The library used for writing config file is Configparser用于编写配置文件的库是 Configparser

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.

相关问题 运行python3代码时在Hackerearth上出现运行时错误NZEC - Runtime Error NZEC on Hackerearth while running python3 code python3打印函数发出'ascii'编解码器无法编码字符错误 - python3 print function emits 'ascii' codec can't encode character error Python3:UnicodeEncodeError:'ascii' 编解码器无法编码字符 '\\xfc' - Python3: UnicodeEncodeError: 'ascii' codec can't encode character '\xfc' 为什么我的 Jupyter Notebook 即使在 Python3 上也使用 ascii 编解码器? - Why is my Jupyter Notebook using ascii codec even on Python3? Python3'ascii'编解码器不能编码字符 - Python3 'ascii' codec can't encode character urllib请求中的python 3.6 ascii编解码器错误 - python 3.6 ascii codec error in urllib request 在Python3中为print()使用编解码器错误处理程序? - Use codec error handler for print() in Python3? 下载图像时出现Python3代码错误 - Python3 code error while downloading the images 运行日历 api python3 时出现“NoneType”错误 - 'NoneType' error while running calendar api python3 'ascii'编解码器无法解码Sublime Text 3上的字节0xe2 - Python3 - 'ascii' codec can't decode byte 0xe2 - Python3 on Sublime Text 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM