python 内部使用unicode编码

来源:互联网 发布:数据库中的索引是什么 编辑:程序博客网 时间:2024/05/21 13:39
<?phpclass TestAction extends Action {    public function index(){#$this->show('Hello world');    $name='怠忽待会';echo $name;#$this->assign('data',$name);#$this->display();    }# !/usr/bin/env python# -*- coding: utf-8 -*-import urllib2import urllibimport cookielibimport jsonimport httplibimport reimport requestss=requests.session()print s.headersurl = "http://127.0.0.1/DEVOPS/index.php/Test/index"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',}r = s.get(url,headers=headers)r=r.contentprint rprint type(r)print rprint len(r)返回的是utf8字符串,C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a19.py{'Connection': 'keep-alive', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'User-Agent': 'python-requests/2.18.4'}怠忽待会<type 'str'>怠忽待会12把utf8 字符串,转换为unicoder = s.get(url,headers=headers)r=r.contentprint rprint type(r)print rprint len(r)r=r.decode('utf-8')print rprint type(r)print len(r)C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a19.py{'Connection': 'keep-alive', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'User-Agent': 'python-requests/2.18.4'}怠忽待会<type 'str'>怠忽待会12怠忽待会<type 'unicode'>4在cmd运行:# !/usr/bin/env python# -*- coding: utf-8 -*-import urllib2import urllibimport cookielibimport jsonimport httplibimport reimport requestss=requests.session()print s.headersurl = "http://127.0.0.1/DEVOPS/index.php/Test/index"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',}r = s.get(url,headers=headers)r=r.contentprint rprint type(r)print rprint len(r)C:\Users\TLCB\Desktop\python\Python 高级编程\4>python test.py{'Connection': 'keep-alive', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'User-Agent': 'python-requests/2.18.4'}鎬犲拷寰呬細<type 'str'>鎬犲拷寰呬細12C:\Users\TLCB\Desktop\python\Python 高级编程\4>此时返回的是utf8,乱码,需要把utf8转为gbk-------------------------------------------------------------------------------r = s.get(url,headers=headers)r=r.contentprint rprint type(r)print rprint len(r)r=r.decode('utf-8').encode('gbk')print rprint type(r)print len(r)C:\Users\TLCB\Desktop\python\Python 高级编程\4>python test.py{'Connection': 'keep-alive', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'User-Agent': 'python-requests/2.18.4'}鎬犲拷寰呬細<type 'str'>鎬犲拷寰呬細12怠忽待会<type 'str'>8或者 把utf8转为unicoder = s.get(url,headers=headers)r=r.contentprint rprint type(r)print rprint len(r)r=r.decode('utf-8')print rprint type(r)print len(r)C:\Users\TLCB\Desktop\python\Python 高级编程\4>python test.py{'Connection': 'keep-alive', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'User-Agent': 'python-requests/2.18.4'}鎬犲拷寰呬細<type 'str'>鎬犲拷寰呬細12怠忽待会<type 'unicode'>4C:\Users\TLCB\Desktop\python\Python 高级编程\4>

原创粉丝点击