python爬虫编码转换_Python 爬虫遇到形如 小说 的编码如何转换为中文? - SegmentFault 思否...
# tested under python3.4
def convert(s):
s = s.strip('') # 把'長'變成'957f'
s = bytes(r'\u' + s, 'ascii') # 把'957f'轉換成b'\\u957f'
return s.decode('unicode_escape') # 調用bytes對象的decode,encoding用unicode_escape,把b'\\u957f'從unicode轉義編碼解碼成unicode的'長'。具體參見codecs的文檔
print(convert('長')) # => '長'
全篇替換
import re
print(re.sub(r'....;',
lambda match: convert(match.group()),
ss))
全文替換后的結果:
學科主題:長篇小說-中國-當代中圖法分類號:# for python2.7
def convert(s):
return ''.join([r'\u', s.strip('')]).decode('unicode_escape')
ss = unicode(ss, 'gbk') # convert gbk-encoded byte-string ss to unicode string
import re
print re.sub(r'....;', lambda match: convert(match.group()), ss)
總結
以上是生活随笔為你收集整理的python爬虫编码转换_Python 爬虫遇到形如 小说 的编码如何转换为中文? - SegmentFault 思否...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图像处理之简化色彩(含OpenCV代码)
- 下一篇: 软件测试桌面检查,静态测试的主要方法 -