2009-05-04 48 views
1

是否有可以檢測(也許是解碼)字符串編碼的Python庫?使用QString編碼

我發現chardet但它給了我一個錯誤,使用:

chardet.detect(self.ui.TextFrom.toPlainText()) 
got: = chardet.detect(self.ui.TextFrom.toPlainText()) 
File .... u.feed(aBuf) File .... 
if self._highBitDetector.search(aBuf): 

TypeError: buffer size mismatch 

另外:

print type(self.ui.TextFrom.toPlainText()) 
# <class 'PyQt4.QtCore.QString'> 
+1

你需要解釋爲什麼chardet的是不是你想要的 - 這是你問什麼了。 – RichieHindle 2009-05-04 08:54:11

+0

對不起,也許它不正確,chardet不是我所需要的。我這個LIB過程中有錯誤,可以使用: chardet.detect(self.ui.TextFrom.toPlainText()) 了: = chardet.detect(self.ui.TextFrom.toPlainText()) 文件.... u.feed(aBuf) 文件.... if self._highBitDetector.search(aBuf): TypeError:緩衝區大小不匹配 – Ockonal 2009-05-04 08:56:47

+0

您在發佈的所有代碼中遇到錯誤,包括self.ui.TextFrom .toPlainText() - 你確定沒有給你一個Unicode字符串嗎?或者根本不是字符串的東西?什麼是打印類型(self.ui.TextFrom.toPlainText())給? – RichieHindle 2009-05-04 09:59:25

回答

7

你需要將它傳遞給chardet之前,您QString轉換爲Python字符串。更改此:

chardet.detect(self.ui.TextFrom.toPlainText()) 

這樣:

chardet.detect(str(self.ui.TextFrom.toPlainText())) 
2

我想這是另一種選擇。

http://cthedot.de/encutils/

A collection of helper functions to detect encodings of text files (like HTML, XHTML, XML, CSS, etc.) retrieved via HTTP, file or string.