python chardet簡單應用

來源:互聯網
上載者:User

標籤:

python的字串編碼識別模組(第三方庫):

官方地址: http://pypi.python.org/pypi/chardet  import chardet import urllib   # 可根據需要,選擇不同的資料 TestData = urllib.urlopen(‘http://www.baidu.com/‘).read() print chardet.detect(TestData)   # 運行結果: # {‘confidence‘: 0.99, ‘encoding‘: ‘GB2312‘}運行結果表示有99%的機率認為這段代碼是GB2312編碼方式。  import urllib from chardet.universaldetector import UniversalDetector usock = urllib.urlopen(‘http://www.baidu.com/‘) # 建立一個檢測對象 detector = UniversalDetector() for line in usock.readlines(): # 分塊進行測試,直到達到閾值 detector.feed(line) if detector.done: break # 關閉檢測對象 detector.close() usock.close() # 輸出檢測結果 print detector.result   # 運行結果: # {‘confidence‘: 0.99, ‘encoding‘: ‘GB2312‘}應用背景,如果要對一個大檔案進行編碼識別,使用這種進階的方法,可以唯讀一部,去判別編碼方式從而提高檢測速度。如果希望使用一個檢測對象檢測多個資料,在每次檢測完,一定要運行一下detector.reset()。清除之前的資料。

python chardet簡單應用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.