日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python中文乱码 def decode-python处理一些乱码的中文文本时decode('utf-8')报错的处理...

發布時間:2023/12/15 python 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中文乱码 def decode-python处理一些乱码的中文文本时decode('utf-8')报错的处理... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

用python寫腳本時,遇到處理中文(亂碼的中文)時,用decode("utf-8")會發現始終會報錯

>>> txt_from = open("/home/love/ex130705.log")

>>> txt_from_iter= iter(txt_from)

>>> txt_proc = txt_from_iter.next().decode("utf-8", "ignore")

Traceback (most recent call last):

File "/tmp/py4049kjX", line 41, in

txt_proc = txt_from_iter.next().decode("utf-8")

File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode

return codecs.utf_8_decode(input, errors, True)

UnicodeDecodeError: "utf8" codec can"t decode bytes in position 84-85: invalid continuation byte

欲處理的原文件中部分顯示為亂碼:

2013-07-05 04:20:10 192.168.1.5 GET /Portals/0/鏁欒偛淇℃伅鏂囦歡澶 校園 80 - 25.XXX.10.99 Mozilla/4.0+(compatible;+MSIE+8.0;+Windows+NT+5.1;+Trident/4.0;+Alexa+Toolbar) 404 0 2 234

2013-07-05 04:20:24 192.168.1.5 GET /Portals/0/鏁欒偛淇℃伅鏂囦歡澶 校園 80 - 25.XXX.10.99 Mozilla/4.0+(compatible;+MSIE+8.0;+Windows+NT+5.1;+Trident/4.0;+Alexa+Toolbar) 404 0 2 296

這些顯示亂碼的中文字符是IIS在記錄日志過程中出現的。python通過decode("utf-8")解碼為UTF-8時會拋出異常UnicodeDecodeError。

解決:用 decode("utf-8", "ignore")

>>>

>>> txt_proc = txt_from_iter.next().decode("utf-8", "ignore")

>>>

查看decode的幫助:

help("".decode)

decode(...)

S.decode([encoding[,errors]]) -> object

Decodes S using the codec registered for encoding. encoding defaults

to the default encoding. errors may be given to set a different error

handling scheme. Default is "strict" meaning that encoding errors raise

a UnicodeDecodeError. Other possible values are "ignore" and "replace"

as well as any other name registered with codecs.register_error that is

able to handle UnicodeDecodeErrors.

總結

以上是生活随笔為你收集整理的python中文乱码 def decode-python处理一些乱码的中文文本时decode('utf-8')报错的处理...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。