[英]Python error : UnicodeEncodeError: 'ascii' codec can't encode character
I have a python script in which a function prints some lines from error file. 我有一个python脚本,其中一个函数从错误文件中打印一些行。
Getting below error when I execute the script through jenkins. 通过jenkins执行脚本时出现以下错误。
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)
Default encoding for python is UTF-8 python的默认编码为UTF-8
>>> import sys
>>> sys.getdefaultencoding()
'utf-8'
I tried to export the variable PYTHONIOENCODING=UTF-8
before executing the script. 在执行脚本之前,我尝试导出变量PYTHONIOENCODING=UTF-8
。
Added below line at the start of script- 在脚本开头的以下行添加了以下内容:
# 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
you can try to use: 您可以尝试使用:
print('ERROR:' + msg.encode('ascii', 'ignore').decode('ascii'))
More info : UnicodeEncodeError 更多信息: UnicodeEncodeError
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.