简体   繁体   English

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

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

相关问题 UnicodeEncodeError:'ascii'编解码器不能编码字符[...] - UnicodeEncodeError: 'ascii' codec can't encode character […] 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 UnicodeEncodeError:'ascii'编解码器无法编码字符错误 - UnicodeEncodeError: 'ascii' codec can't encode character error Python错误; UnicodeEncodeError:'ascii'编解码器无法编码字符u'\ u2026' - Python Error; UnicodeEncodeError: 'ascii' codec can't encode character u'\u2026' Python unicode错误。 UnicodeEncodeError:'ascii'编解码器无法编码字符u'\\ u4e3a' - Python unicode error. UnicodeEncodeError: 'ascii' codec can't encode character u'\u4e3a' MongoDB中的Python错误“ UnicodeEncodeError:'charmap'编解码器无法编码字符” - Python error “UnicodeEncodeError: 'charmap' codec can't encode character” in MongoDB UnicodeEncodeError:'ascii'编解码器不能编码字符u'\\ xe4' - UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' UnicodeEncodeError:'ascii'编解码器不能编码字符u'\\ xef' - UnicodeEncodeError: 'ascii' codec can't encode character u'\xef' UnicodeEncodeError: 'ascii' 编解码器无法在打印功能中编码字符 - UnicodeEncodeError: 'ascii' codec can't encode character in print function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM