0

私の問題は、PythonがSQLテーブルの列の文字エンコーディングでうまく機能しないことだと思います:

| column | varchar(255) | latin1_swedish_ci | YES  |     | NULL              |                             | select,insert,update,references |    | 

上記は、この列の出力を示しています。タイプvarchar(255)があり、エンコーディングがありますlatin1_swedish_ci.

このデータで Python を再生しようとすると、次のエラーが発生します。

 dictionary = gs.corpora.Dictionary(tweets)
  File "/usr/local/lib/python2.7/dist-packages/gensim-0.9.1-py2.7.egg/gensim/corpora/dictionary.py", line 50, in __init__
    self.add_documents(documents)
  File "/usr/local/lib/python2.7/dist-packages/gensim-0.9.1-py2.7.egg/gensim/corpora/dictionary.py", line 97, in add_documents
    _ = self.doc2bow(document, allow_update=True) # ignore the result, here we only care about updating token ids
  File "/usr/local/lib/python2.7/dist-packages/gensim-0.9.1-py2.7.egg/gensim/corpora/dictionary.py", line 121, in doc2bow
    document = sorted(utils.to_utf8(token) for token in document)
  File "/usr/local/lib/python2.7/dist-packages/gensim-0.9.1-py2.7.egg/gensim/corpora/dictionary.py", line 121, in <genexpr>
    document = sorted(utils.to_utf8(token) for token in document)
  File "/usr/local/lib/python2.7/dist-packages/gensim-0.9.1-py2.7.egg/gensim/utils.py", line 164, in any2utf8
    return unicode(text, encoding, errors=errors).encode('utf8')
  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 byte 0x96 in position 0: invalid start byte

gsgensimトピック モデリング ライブラリです。問題は、gensim が Unicode エンコーディングを必要とすることだと思います。

  1. データベース内のこの列の文字エンコーディング (照合?) を変更するにはどうすればよいですか?
  2. 代替ソリューションはありますか?

助けてくれてありがとう!

4

3 に答える 3

3

あなたのMYSQLdb pythonライブラリは、utf8にエンコードすることになっていることを認識していないと思います

デフォルトのpythonシステム定義の文字セットlatin1にエンコードされています。

データベースに connect() するときは、 charset='utf8'

パラメータ。これもマニュアル化すべきSET NAMES

于 2014-04-28T18:55:10.937 に答える
0

MySQLdb v1.2.5で@saudi_Devのソリューションを試しました。クエリを実行したテーブルは で作成されましたDEFAULT CHARSET=utf8。それでも、@saudi_Dev の解決策を試す前に、何らかの理由cursor.fetchall()で文字列が返されました。パラメータlatin1を使用した後、文字列を ではなく(技術的には ではありません)として返します。charset=utf8cursor.fetchall()Unicodeutf8latin1

しかし、 http://mysql-python.sourceforge.net/MySQLdb.htmlで、パラメータを渡すこともできることを見てきましたuse_unicode=False。これは、私が投稿したリンクのユーザーズガイドによると、charsetパラメーターを使用するとuse_unicode=True.

于 2018-12-07T08:15:15.033 に答える