Robot Framework:日志输出中文Unicode编码

robotframework 输出日志时,中文显示为Unicode编码 。

修改方法:

在Python27\Lib\site-packages\robotframework-3.0.4-py2.7.egg\robot\utils\unic.py文件中添加以下代码

import json

一定要导入包,否则虽然不报错,但是不能解决问题

if PY2:

    def unic(item):
        if isinstance(item, unicode):
            return item
        if isinstance(item, (bytes, bytearray)):
            try:
                return item.decode('ASCII')
            except UnicodeError:
                return u''.join(chr(b) if b < 128 else '\\x%x' % b
                                for b in bytearray(item))

        # 添加内容
        if isinstance(item, (list, dict, tuple)):
            try:
                item = json.dumps(item, ensure_ascii=False, encoding='utf-8')
            except UnicodeDecodeError:
                try:
                    item = json.dumps(item, ensure_ascii=False, encoding='gbk')
                except:
                    pass
            except:
                pass
        ##### 
        try:
            try:
                return unicode(item)
            except UnicodeError:
                return unic(str(item))
        except:
            return _unrepresentable_object(item)

修改后,重启robotframework后,输出日志正常

猜你喜欢

转载自www.cnblogs.com/rechin/p/10075413.html