- 时间:2020-10-24 19:05 编辑: 来源: 阅读:
- 扫一扫,手机访问
摘要:python用于url解码和中文解析的小脚本(python url decoder)
[url=http://yac163.svn.sourceforge.net/viewvc/yac163/trunk/yac163-nox/Pic.py?revision=198&view=markup]http://yac163.svn.sourceforge.net/viewvc/yac163/trunk/yac163-nox/Pic.py?revision=198&view=markup[/url]
參考我這個很古老code裡面的#102-147行 給每個decode和encode調用加上(…,”ignore”)。
def strdecode( string,charset=None ):
if isinstance(string,unicode):
return string
if charset:
try:
return string.decode(charset)
except UnicodeDecodeError:
return _strdecode(string)
else:
return _strdecode(string)
def _strdecode(string):
try:
return string.decode('utf8')
except UnicodeDecodeError:
try:
return string.decode('gb2312')
except UnicodeDecodeError:
try:
return string.decode('gbk')
except UnicodeDecodeError:
return string.decode('gb18030')
def strencode( string,charset=None ):
if isinstance(string,str):
return string
if charset:
try:
return string.encode(charset)
except UnicodeEncodeError:
return _strencode(string)
else:
return _strencode(string)
def _strencode(string):
try:
return string.encode('utf8')
except UnicodeEncodeError:
try:
return string.encode('gb2312')
except UnicodeEncodeError:
try:
return string.encode('gbk')
except UnicodeEncodeError:
return string.encode('gb18030')