Skip to main content
 首页 » 编程设计

python之使用 QString 编码

2025年05月04日9lori

是否有可以检测(并可能解码)字符串编码的 Python 库?

我找到了 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'> 

请您参考如下方法:

在将它传递给 chardet 之前,您需要将您的 QString 转换为 Python 字符串。改变这个:

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

为此:

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