• 字符串继续编码 报UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in rang


    dd_/root>cat a2.py 
    import commands
    import MySQLdb
    condition='20.2.224.26'
    conn= MySQLdb.connect(
            host='127.0.0.1',
            port = 3306,
            user='root',
            passwd='1234567',
            db ='DEVOPS',
            charset="UTF8",
            )
    cur = conn.cursor()
    a = cur.execute("select user,password,name,ip,port from mon_activemq  where ip=%s ",[condition])
    info = cur.fetchone()
    print info
    user=info[0].encode('utf-8')
    code=info[1].encode('utf-8')
    app=info[2].encode('utf-8')
    ip=info[3].encode('utf-8')
    port=info[4].encode('utf-8')
    print user
    print type(user)
    print app
    print type(app)
    output=commands.getoutput("/home/mqm/sbin/activemq/view_activemq %s %s %s %s %s "  %(user,code,app,ip,port) )
    print output
    print type(output)
    output=output.rstrip('||')
    print output
    print type(output)
    arr1=[]
    arr2=[]
    arr3=[]
    arr1=output.split('||')
    print arr1
    print type(arr1)
    for x in arr1:
        arr2=x.split('->')
        arr3.append(arr2)
    print '---------------------'
    print arr3
    print type(arr3)
    print arr3[0]
    print type(arr3[0][0])
    print len(arr3[0][0])
    print arr3[0][0].decode('utf-8')
    print type(arr3[0][0].decode('utf-8'))
    print len(arr3[0][0].decode('utf-8'))
    print arr3[0][0].encode('utf-8')
    
    
    <type 'str'>
    13
    消息平台B
    <type 'unicode'>
    5
    
    已经是字符串了 就不能再encode了  unicode 可以encode utf8
    
    
    
    
    dd_/root>cat a2.py 
    # !/usr/bin/env python
    # -*- coding: utf-8 -*-
    a='测试'
    print a
    print type(a)
    print len(a)
    print '2222222222222222'
    b=a.encode('utf-8')
    
    
    dd_/root>python a2.py
    测试
    <type 'str'>
    6
    2222222222222222
    Traceback (most recent call last):
      File "a2.py", line 8, in <module>
        b=a.encode('utf-8')
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128)
    
    
    字符串是无法再继续编码成utf-8的 
    dd_/root>python a2.py 
    测试
    <type 'str'>
    6
    2222222222222222
    测试
    <type 'unicode'>
    2
    
    
    dd_/root>cat a2.py 
    # !/usr/bin/env python
    # -*- coding: utf-8 -*-
    import sys
    reload(sys)
    sys.setdefaultencoding('utf8')
    a='测试'
    print a
    print type(a)
    print len(a)
    print '2222222222222222'
    b=a.encode('utf-8')
    print b
    print type(b)
    print len(b)
    
    dd_/root>python a2.py 
    测试
    <type 'str'>
    6
    2222222222222222
    测试
    <type 'str'>
    6

  • 相关阅读:
    Linux关闭防火墙命令
    js改变数组的两个元素的位子,互换、置顶
    vue nexttick的理解和使用场景
    vue mint-ui 框架下拉刷新上拉加载组件的使用
    vue项目中使用了vw适配方案,引入第三方ui框架mint-ui时,适配问题解决
    小程序开发笔记【二】,抽奖结果json数据拼装bug解决
    gulp插件gulp-nunjucks-render的使用及gulp4的简单了解
    小程序开发笔记【一】,查询用户参与活动列表 left join on的用法
    mysql数据插入前判断是否存在
    微信公众号通过图片选取接口上传到阿里oss
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349190.html
Copyright © 2020-2023  润新知