1

I have a python script in which a function prints some lines from error file.

Getting below error when I execute the script through 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

>>> import sys
>>> sys.getdefaultencoding()
'utf-8'

I tried to export the variable PYTHONIOENCODING=UTF-8 before executing the script.

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
Klaus D.
  • 13,874
  • 5
  • 41
  • 48
GKotkar
  • 47
  • 1
  • 3

1 Answers1

0

you can try to use:

print('ERROR:' + msg.encode('ascii', 'ignore').decode('ascii'))

More info : UnicodeEncodeError

C.med
  • 581
  • 1
  • 5
  • 21
  • Thanks for the reply C.med If my default python encoder is UTF-8, why it is throwing such error- 'ascii' codec can't encode character. From where exactly it is picking ascii encoder. – GKotkar Apr 11 '19 at 11:32
  • Yes, your default encoding is utf-8, but you are using an external file. they're two different things. (your file could be encoded differently). Another thing, your msg is `msg = project_info + ': execution failed with error: ' + error + '\n'`. Are you sure that `project_info` is an encoded text? – C.med Apr 11 '19 at 13:17
  • Thanks, above code worked but it ignores the accent characters and does'nt print it – GKotkar Apr 12 '19 at 08:19
  • Yes it's not really recommended to ignore `ascii` like that. Try to reuse your code by adding `# -*- coding: utf-8 -*-` at the beginning of your script. – C.med Apr 12 '19 at 13:15