Skip to content

Instantly share code, notes, and snippets.

@HQMIS
Last active December 10, 2015 08:08
Show Gist options
  • Select an option

  • Save HQMIS/4405267 to your computer and use it in GitHub Desktop.

Select an option

Save HQMIS/4405267 to your computer and use it in GitHub Desktop.
Python 编码, 研究
For IDLE
Python 2.7.2 |EPD 7.2-2 (32-bit)| (default, Sep 14 2011, 11:02:05) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import sys
>>> sys.getdefaultencoding()
'ascii'
>>> str_Chinese = "电源管理"
>>> print "".join(["%"+str(hex(ord(x)))[2:] for x in str_Chinese.decode("gb2312").encode("utf8")])
%e7%94%b5%e6%ba%90%e7%ae%a1%e7%90%86
>>>
For .py
import sys
print sys.getdefaultencoding()
str_Chinese = "电源管理"
print "".join(["%"+str(hex(ord(x)))[2:] for x in str_Chinese.decode("gb2312").encode("utf8")])
#-*-coding:utf-8-*-
str_Chinese = "电源管理"
print "".join(["%"+str(hex(ord(x)))[2:] for x in str_Chinese])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment