|
问题描述:接口、数据库返回信息有中文的时候会显示unicode的样式,如图
解决方法:
1、robotframework为3.0.X
2、找到Python安装目录下的\Lib\site-packages\robot\utils\unic.py文件
引入json库:import json
将下面代码复制到如图位置,注意对齐方式
if isinstance(item, (list, dict, tuple)): try: item = json.dumps(item, ensure_ascii=False, encoding='cp936')
except UnicodeDecodeError: try: item = json.dumps(item, ensure_ascii=False, encoding='cp936') except: pass except: pass
扩展:其中的cp936可用utf-8或者gbk编码格式去替换
也可以下载unic.py文件替换掉
下载地址:unic.py
复制代码
- <p># Copyright 2008-2015 Nokia Networks</p><p># Copyright 2016- Robot Framework Foundation</p><p>#</p><p># Licensed under the Apache License, Version 2.0 (the "License");</p><p># you may not use this file except in compliance with the License.</p><p># You may obtain a copy of the License at</p><p>#</p><p># http://www.apache.org/licenses/LICENSE-2.0</p><p>#</p><p># Unless required by applicable law or agreed to in writing, software</p><p># distributed under the License is distributed on an "AS IS" BASIS,</p><p># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</p><p># See the License for the specific language governing permissions and</p><p># limitations under the License.</p><p>
- </p><p>from pprint import PrettyPrinter</p><p>
- </p><p>from .platform import IRONPYTHON, JYTHON, PY2</p><p>from .robottypes import is_bytes, is_unicode</p><p>import json</p><p>
- </p><p>if PY2:</p><p>
- </p><p> def unic(item):</p><p> if isinstance(item, unicode):</p><p> return item</p><p> if isinstance(item, (bytes, bytearray)):</p><p> try:</p><p> return item.decode('ASCII')</p><p> except UnicodeError:</p><p> return u''.join(chr(b) if b < 128 else '\\x%x' % b</p><p> for b in bytearray(item))</p><p> </p><p> if isinstance(item, (list, dict, tuple)):</p><p> try:</p><p> item = json.dumps(item, ensure_ascii=False, encoding='utf-8')</p><p> except UnicodeDecodeError:</p><p> try:</p><p> item = json.dumps(item, ensure_ascii=False, encoding='gbk')</p><p> except:</p><p> pass</p><p> except:</p><p> pass</p><p> try:</p><p> try:</p><p> return unicode(item)</p><p> except UnicodeError:</p><p> return unic(str(item))</p><p> except:</p><p> return _unrepresentable_object(item)</p><p>
- </p><p>else:</p><p>
- </p><p> def unic(item):</p><p> if isinstance(item, str):</p><p> return item</p><p> if isinstance(item, (bytes, bytearray)):</p><p> try:</p><p> return item.decode('ASCII')</p><p> except UnicodeError:</p><p> return ''.join(chr(b) if b < 128 else '\\x%x' % b</p><p> for b in item)</p><p> try:</p><p> return str(item)</p><p> except:</p><p> return _unrepresentable_object(item)</p><p>
- </p><p>
- </p><p># JVM and .NET seem to handle Unicode normalization automatically. Importing</p><p># unicodedata on Jython also takes some time so it's better to avoid it.</p><p>if not (JYTHON or IRONPYTHON):</p><p>
- </p><p> from unicodedata import normalize</p><p> _unic = unic</p><p>
- </p><p> def unic(item):</p><p> return normalize('NFC', _unic(item))</p><p>
- </p><p>
- </p><p>def prepr(item, width=400):</p><p> return unic(PrettyRepr(width=width).pformat(item))</p><p>
- </p><p>
- </p><p>class PrettyRepr(PrettyPrinter):</p><p>
- </p><p> def format(self, object, context, maxlevels, level):</p><p> try:</p><p> if is_unicode(object):</p><p> return repr(object).lstrip('u'), True, False</p><p> if is_bytes(object):</p><p> return 'b' + repr(object).lstrip('b'), True, False</p><p> return PrettyPrinter.format(self, object, context, maxlevels, level)</p><p> except:</p><p> return _unrepresentable_object(object), True, False</p><p>
- </p><p>
- </p><p>def _unrepresentable_object(item):</p><p> from .error import get_error_message</p><p> return u"<Unrepresentable object %s. Error: %s>" \</p><p> % (item.__class__.__name__, get_error_message())</p>
复制代码
复制代码
修改后运行脚本显示结果如下:
|
|