python - Python错误:UnicodeEncodeError:'ascii'编解码器无法编码字符
问题描述
我有一个 python 脚本,其中一个函数从错误文件中打印一些行。
当我通过詹金斯执行脚本时出现以下错误。
release/bin/eat2/eat.py", line 553, in _runtest
print('ERROR:' + msg)
UnicodeEncodeError: 'ascii' codec can't encode character '\u0447' in position 315: ordinal not in range(128)
python的默认编码是UTF-8
>>> import sys
>>> sys.getdefaultencoding()
'utf-8'
我尝试PYTHONIOENCODING=UTF-8
在执行脚本之前导出变量。
在脚本开头添加以下行 -
# coding: utf8
def _check_gpderrors(gdplogfile):
LOGERROR_REGEX = re.compile("^\d+-\d+-\d+ \d+:\d+:\d+ Error:")
errors = []
import codecs
f = codecs.open(logfile, 'r', encoding='utf-8')
for line in f:
if re.match(LOGERROR_REGEX, line):
errors.append(line.strip())
f.close()
return errors
errors = {}
errors = _check_gdperrors(log_file)
for error in errors:
msg = project_info + ': execution failed with error: ' + error + '\n'
print('ERROR:' + msg)
logs.append(msg)
script_error = True
解决方案
推荐阅读
- javascript - Firebase orderByKey().startAt() 未按预期工作。怎么了?
- https - 如何为 GKE Ingress 设置 TLS 连接
- vue.js - 将 vue 组件发布到 npm 时需要添加哪些依赖项?
- java - 在 Spring MVC 应用程序上获取空的招摇 UI
- dart - Flutter - 在同一页面上添加卡片列表和文本后出现异常
- docker - 在 docker 中处理服务配置的最佳实践
- c - 在 C 中的预增量运算符中获得更多增量输出
- charts - 显示 Google 表格数据集中数字数量的图表
- javascript - 我应该如何获取表单的输入,并将该输入发送到 C# 类?ASP.Net Core - 剃刀页面
- mysql - 在 Create Table 语句中使用用户定义的变量来指定 varchar 长度