Filemaker to Python (3) - 途切れた文字列
前回、PythonからFilemakerのテーブルにアクセスする事に成功はしたのですが、検索した文字列の2文字目以降が途切れてしまうという問題が発生しています。ちなみにODBC側でログを取るオプションを使っても何も吐き出されていませんでした。
ちょっと実験
Select文の検索条件を付けてみましょう。
>>> cursor.execute("select Word, Pinyin from CWordsDB where Pinyin ='ban1 yun4 gong1'") <pyodbc.Cursor object at 0x100763570> >>> rows = cursor.fetchall() >>> for row in rows: ... print row.Word, row.Pinyin ... 搬 ban1 yun4 gong1
やはり、単語(Word)については最初の1文字だけが表示されています。じゃ、検索条件で使うとどうなるんでしょうか?
>>> cursor.execute("select Word, Pinyin from CWordsDB where Word ='搬运工'") <pyodbc.Cursor object at 0x100763570> >>> for row in rows: ... print row.Word, row.Pinyin ... 搬 ban1 yun4 gong1 >>>
検索そのものは成功している様子。てか文字列変換なしでそのまま行けてるのが軽い驚き。
別のデータではどうなるか?
この「搬运工」という単語、我が単語DBの先頭に乗っているから使っていたのですが、一応他の単語ではどうなるかを検証してみること、意外な事が分かりました。
>>> cursor.execute("select Word, Pinyin from CWordsDB where Word ='上海'") <pyodbc.Cursor object at 0x100763570> >>> rows = cursor.fetchall() >>> for row in rows: ... print row.Word, row.Pinyin ... ? shang4 hai3 >>> cursor.execute("select Word, Pinyin from CWordsDB where Word ='绅士'") <pyodbc.Cursor object at 0x100763570> >>> rows = cursor.fetchall() >>> for row in rows: ... print row.Word, row.Pinyin ... ? shen1 shi4 >>> cursor.execute("select Word, Pinyin from CWordsDB where Word ='搬运工'") <pyodbc.Cursor object at 0x100763570> >>> rows = cursor.fetchall() >>> for row in rows: ... print row.Word, row.Pinyin ... 搬 ban1 yun4 gong1 >>> cursor.execute("select Word, Pinyin from CWordsDB where Word ='烈日'") <pyodbc.Cursor object at 0x100763570> >>> rows = cursor.fetchall() >>> for row in rows: ... print row.Word, row.Pinyin ... ? lie4 ri4 >>>
どうも、どの単語も最初の1文字が表示されるという訳ではなさそうです。例えば、この直前で扱った「烈日」という単語について、生のデータはどうなっているかを拾ってみると、こうなっていました。
>>> row.Word '\xe7\x83'
中国語の漢字の場合、”\x”で始まる文字列が3ブロック分あって漢字1文字に相当します。しかし、それが2ブロック分しか取得できていない。ということなんでしょうか?
思い切って、全単語を出力してみました。
>>> cursor.execute("select Word, Pinyin from CWordsDB") <pyodbc.Cursor object at 0x100763570> >>> rows = cursor.fetchall() >>> for row in rows: ... print row.Word, row.Pinyin ... 搬 ban1 yun4 gong1 ? lie4 ri4 挥? hui1 han4 ru2 yu3 ? kan4 zhong4 ? fa1 hui1 ? ning4 ke3 ? mou2 sheng1 ? shou3 duan4 ? shou4 zui4 ? qing1 xiang4 ? pin1 ming4 ? shi4 dang4 ....
漢字によっては、きちんと表示されるパターンもあるようですが、大部分はダメです。
そこで頭をよぎった事がもう1つ。ODBC Adminの中でのSpecial languageの設定です。アレをいじる前は、こんな漢字で出力されていました。
('???', 'ban1 yun4 gong1')
文字化けはしているけど、文字分として認識はされていました。という事が言語設定が逆に悪さをしているのかもしれません。
再び言語設定を変更してみた
今度は、Treat text type as Unicodeのオプションを外し、Multi-byte text codingにUTF-8を指定してみました。これでどうなるか実験です。
>>> cxcn = pyodbc.connect('DSN=CWord;UID=Admin;PWD=Admin') >>> cursor = cxcn.cursor() >>> cursor.execute("select Word, Pinyin from CWordsDB where Word ='搬运工'") <pyodbc.Cursor object at 0x100763c90> >>> rows = cursor.fetchall() >>> for row in rows: ... print row.Word, row.Pinyin ... 搬运工 ban1 yun4 gong1 >>> cursor.execute("select Word, Pinyin from CWordsDB where Word ='烈日'") <pyodbc.Cursor object at 0x100763c90> >>> rows = cursor.fetchall() >>> for row in rows: ... print row.Word, row.Pinyin ... 烈日 lie4 ri4 >>>
問題なく動きました。これでいろいろいじる事が出来そうです。