zoukankan      html  css  js  c++  java
  • 字符串继续编码 报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

  • 相关阅读:
    js 自动下载函数
    集群中用Memcached来实现session共享
    PDO防注入原理分析以及使用PDO的注意事项
    侧边栏
    helloworld
    angularjs 获取地址传参
    ionic 上拉加载更多&瀑布流加载&滚动到底部加载更多 主意事项
    亿级Web系统搭建——单机到分布式集群
    php 模拟表单提交
    R语言curve绘图函数
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349190.html
Copyright © 2011-2022 走看看