我正在使用 Visual Studio 的 Python 工具并阅读一些用意大利语编写的文件。试过 iso-8859-1、iso-8859-2、utf-8、utf-8-sig。Notepad++ 以不带 BOM 的 UTF-8 格式打开文件。
content = fp.read()
words = content.decode("utf-8-sig").lower().split()
for w in words:
p=''
cur.execute('SELECT word FROM multiwordnet.italian_lemma l, multiwordnet.italian_synset s where l.id = s.id and l.lemma="%s"' % w)
导致崩溃的字符串是C'è
. (读作"c\'\xe3\xa8"
)
使用 chardet 没有帮助
Traceback (most recent call last):
File "C:\Users\Tathagata\Documents\Visual Studio 2012\Projects\PythonApplicati
on4\PythonApplication4\PythonApplication4.py", line 344, in <module>
createSynsetDict()
File "C:\Users\Tathagata\Documents\Visual Studio 2012\Projects\PythonApplicati
on4\PythonApplication4\PythonApplication4.py", line 294, in createSynsetDict
cur.execute('SELECT word FROM multiwordnet.italian_lemma l, multiwordnet.it
alian_synset s where l.id = s.id and l.lemma="%s"' % w)
File "C:\Python27\lib\site-packages\pymysql\cursors.py", line 117, in execute
self.errorhandler(self, exc, value)
File "C:\Python27\lib\site-packages\pymysql\connections.py", line 187, in defa
ulterrorhandler
raise Error(errorclass, errorvalue)
Error: (<type 'exceptions.UnicodeEncodeError'>, UnicodeEncodeError('ascii', u's\
x00\x00\x00\x03SELECT word FROM multiwordnet.italian_lemma l, multiwordnet.ital
ian_synset s where l.id = s.id and l.lemma="c\'\xe3\xa8"', 116, 118, 'ordinal no
t in range(128)'))