zoukankan      html  css  js  c++  java
  • python unicode 转utf8,转gbk

    filename='E:image.jpg'
    local = open(filename,'wb')
    local.write(r)
    local.close()
    print "登录二维码已经下载到本地"+"["+filename+"]"
    
    ##打开图片
    os.system("start %s" % filename);
    code = raw_input('输入验证码: ')
    print code;
    print len(code)
    
    login_url='https://www.awfae.com/business/dispatch_post.do'
    data = {'action':'doLogin','userName': '18072722237','followId':'','userPass':'79e8fe923f031f46221a9283d0c9263a','validCode':code}
    #data = urllib.urlencode(data)
    headers={
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    }
    response = s.post(login_url, data=data,headers=headers)
    print  response
    print response.status_code
    print response.content
    #返回的跳转URL
    print response.url
    repurl='https://www.awfae.com/web/auth?method=%2Fclient%2Fassetlist&platform=web&_=1508406995591'
    r= s.get(repurl,headers=headers)
    r = r.content
    print r
    print type(r)
    print type(r)
    text = json.loads(r)
    print type(text)
    print text['data']
    print type( text['data'])
    print text['data'][0]
    print '--------------------'
    print text['data'][0]['exchangeName']
    print type(text['data'][0]['exchangeName'])
    print len(text['data'][0]['exchangeName'])
    print text['data'][0]['exchangeName'].encode('utf-8')
    print type(text['data'][0]['exchangeName'].encode('utf-8'))
    print len(text['data'][0]['exchangeName'].encode('utf-8'))
    
    
    
    在PyCharm返回的是unicode把,unicode转换成utf-8
    <type 'str'>
    <type 'str'>
    <type 'dict'>
    [{u'disableBuyAmount': 0, u'totalAvailableBalance': 8.0, u'wzeFrozenBalance': 0.0, u'canRechargeAmount': 0, u'exchangeCode': u'10000001', u'clientSn': 1662, u'frozenWithdraw': 0.0, u'currentInvestBalance': 0, u'todayCashLimit': True, u'availableBalance': 6.1, u'exchangeName': u'u6e29u91d1u6240', u'wzeAvailableBalance': 1.9, u'totalInvestBalance': 0.0, u'frozenPay': 0.0}, {u'disableBuyAmount': 0, u'totalAvailableBalance': 0.0, u'wzeFrozenBalance': 0.0, u'canRechargeAmount': 0, u'exchangeCode': u'10000002', u'clientSn': 1662, u'frozenWithdraw': 0.0, u'currentInvestBalance': 0, u'todayCashLimit': True, u'availableBalance': 0.0, u'exchangeName': u'u9547u91d1u6240', u'wzeAvailableBalance': 0.0, u'totalInvestBalance': 0.0, u'frozenPay': 0.0}]
    <type 'list'>
    {u'disableBuyAmount': 0, u'totalAvailableBalance': 8.0, u'wzeFrozenBalance': 0.0, u'canRechargeAmount': 0, u'exchangeCode': u'10000001', u'clientSn': 1662, u'frozenWithdraw': 0.0, u'currentInvestBalance': 0, u'todayCashLimit': True, u'availableBalance': 6.1, u'exchangeName': u'u6e29u91d1u6240', u'wzeAvailableBalance': 1.9, u'totalInvestBalance': 0.0, u'frozenPay': 0.0}
    --------------------
    温金所
    <type 'unicode'>
    3
    温金所
    <type 'str'>
    9
    
    
    在cmd运行:
    
    {u'disableBuyAmount': 0, u'totalAvailableBalance': 8.0, u'wzeFrozenBalance': 0.0
    , u'canRechargeAmount': 0, u'exchangeCode': u'10000001', u'clientSn': 1662, u'fr
    ozenWithdraw': 0.0, u'currentInvestBalance': 0, u'todayCashLimit': True, u'avail
    ableBalance': 6.1, u'exchangeName': u'u6e29u91d1u6240', u'wzeAvailableBalance
    ': 1.9, u'totalInvestBalance': 0.0, u'frozenPay': 0.0}
    --------------------
    温金所
    <type 'unicode'>
    3
    娓╅噾鎵€
    <type 'str'>
    9
    
    
    print r
    print type(r)
    print type(r)
    text = json.loads(r)
    print type(text)
    print text['data']
    print type( text['data'])
    print text['data'][0]
    print '--------------------'
    print text['data'][0]['exchangeName']
    print type(text['data'][0]['exchangeName'])
    print len(text['data'][0]['exchangeName'])
    print text['data'][0]['exchangeName'].encode('gbk')
    print type(text['data'][0]['exchangeName'].encode('gbk'))
    print len(text['data'][0]['exchangeName'].encode('gbk'))
    
    unicode 转成gbk:
    
    {u'disableBuyAmount': 0, u'totalAvailableBalance': 8.0, u'wzeFrozenBalance': 0.0
    , u'canRechargeAmount': 0, u'exchangeCode': u'10000001', u'clientSn': 1662, u'fr
    ozenWithdraw': 0.0, u'currentInvestBalance': 0, u'todayCashLimit': True, u'avail
    ableBalance': 6.1, u'exchangeName': u'u6e29u91d1u6240', u'wzeAvailableBalance
    ': 1.9, u'totalInvestBalance': 0.0, u'frozenPay': 0.0}
    --------------------
    温金所
    <type 'unicode'>
    3
    温金所
    <type 'str'>
    6
    


    
    
    
                                        
    
  • 相关阅读:
    多线程执行有返回值有参数的方法
    当连续进行多个请求,并且请求的url地址相同时。放弃前面的所有请求,只执行最后一次请求。
    防止重复发送Ajax请求的解决方案
    多行文本溢出显示省略号
    h5 文件跨域上传
    完美解决 IOS系统safari5.0 浏览器页面布局iframe滚动条失效问题,iossafari5.0
    CSS3 修改和去除移动端点击事件出现的背景框 (tap-highlight-color)
    去除img之间的空白
    手机上点击a标签是出现阴影解决办法
    idea通过maven构建springMVC+mybatis项目
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349499.html
Copyright © 2011-2022 走看看