zoukankan      html  css  js  c++  java
  • robotframework中文显示乱码

    问题描述:接口、数据库返回信息有中文的时候会显示unicode的样式,如图 

    image

    解决方法: 

    1、robotframework为3.0.X

    2、找到Python安装目录下的Libsite-packages obotutilsunic.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

    213103814693

    扩展:其中的cp936可用utf-8或者gbk编码格式去替换 

    也可以下载unic.py文件替换掉 

    下载地址:unic.py

    #  Copyright 2008-2015 Nokia Networks
    #  Copyright 2016-     Robot Framework Foundation
    #
    #  Licensed under the Apache License, Version 2.0 (the "License");
    #  you may not use this file except in compliance with the License.
    #  You may obtain a copy of the License at
    #
    #      http://www.apache.org/licenses/LICENSE-2.0
    #
    #  Unless required by applicable law or agreed to in writing, software
    #  distributed under the License is distributed on an "AS IS" BASIS,
    #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    #  See the License for the specific language governing permissions and
    #  limitations under the License.
    
    from pprint import PrettyPrinter
    
    from .platform import IRONPYTHON, JYTHON, PY2
    from .robottypes import is_bytes, is_unicode
    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)
    
    else:
    
        def unic(item):
            if isinstance(item, str):
                return item
            if isinstance(item, (bytes, bytearray)):
                try:
                    return item.decode('ASCII')
                except UnicodeError:
                    return ''.join(chr(b) if b < 128 else '\x%x' % b
                                   for b in item)
            try:
                return str(item)
            except:
                return _unrepresentable_object(item)
    
    
    # JVM and .NET seem to handle Unicode normalization automatically. Importing
    # unicodedata on Jython also takes some time so it's better to avoid it.
    if not (JYTHON or IRONPYTHON):
    
        from unicodedata import normalize
        _unic = unic
    
        def unic(item):
            return normalize('NFC', _unic(item))
    
    
    def prepr(item, width=400):
        return unic(PrettyRepr(width=width).pformat(item))
    
    
    class PrettyRepr(PrettyPrinter):
    
        def format(self, object, context, maxlevels, level):
            try:
                if is_unicode(object):
                    return repr(object).lstrip('u'), True, False
                if is_bytes(object):
                    return 'b' + repr(object).lstrip('b'), True, False
                return PrettyPrinter.format(self, object, context, maxlevels, level)
            except:
                return _unrepresentable_object(object), True, False
    
    
    def _unrepresentable_object(item):
        from .error import get_error_message
        return u"<Unrepresentable object %s. Error: %s>" 
               % (item.__class__.__name__, get_error_message())

    修改后运行脚本显示结果如下: 

    213104152182

  • 相关阅读:
    500内部服务器错误
    silverlight中实现页面传值
    每个程序员必备的10种工具
    ora-12514:TNS:listener does not currently know of service requested in connect descriptor
    利用VBA 宏实现vc6.0的自动添加注释和自动取消注释
    plsql developer (在8.04版本中试过可用)
    修改Oracle数据库字符集
    80端口被占用(端口检查)解决
    sql server分页
    解决问题 “You don't have permission to access /index.html on this server.”
  • 原文地址:https://www.cnblogs.com/linkxu1989/p/6640560.html
Copyright © 2011-2022 走看看