2017-06-21 14 views
0

このエラーの内容と解決方法はわかりません。TypeError:文字マッピングで整数、なし、またはユニコードを返す必要があります

texts = [[word for word in document.translate(trans_table).lower().split()] for document in live_text] 

TypeError: character mapping must return integer, None or unicode 

マイコード:ウェブ経由

rows=cursor.fetchall() 
listTSeps=[] 

for row in rows: 
    listTSeps.append(re.sub('[^A-Za-z0-9]+', ' ', row[0])) 

#Close cursor and connection done reading from database 
cursor.close() 
conn.close() 


live_text=listTSeps 
trans_table = ''.join([chr(i) for i in range(128)] + [' '] * 128) 

texts = [[word for word in document.translate(trans_table).lower().split()] for document in live_text] 

text_matrix = ["None"]*len(live_text) 

私の検索では、これが.encode( 'アスキー')またはORD()を使用して解決することができると結論づけました。

私はパイソンと素人であり、サンプルコードから学びたいと思っています。私は友人からこれを見た。誰かが問題の原因を説明するのに十分なほど親切で、どうすれば修正できるのでしょうか?ありがとう。

+0

それはエラーをスローしますか? –

+0

ここに: texts = [[live_textのドキュメント用のdocument.translate(trans_table).lower()。split()の単語の単語] –

+1

あなたのPythonのバージョンは? Python 3.0〜3.3では、変換テーブルの文字列は許可されていませんでしたが、マップが必要でした。 –

答えて

1

documentunicodeで、strではありません。 unicodeの場合、translate()メソッドは、256文字の文字列ではなく、別のものである必要があります。

help(u' '.translate) 

利回り:

Help on built-in function translate: 

translate(...) 
    S.translate(table) -> unicode 

    Return a copy of the string S, where all characters have been mapped 
    through the given translation table, which must be a mapping of 
    Unicode ordinals to Unicode ordinals, Unicode strings or None. 
    Unmapped characters are left untouched. Characters mapped to None 
    are deleted. 

このような辞書が細かいです:あなただけのスペースでASCII 127以上のすべての文字を置き換えたいあなたのケースのために

u'abcd efgh'.translate({ 32: u'x' }) 
u'abcdxefgh' 

、あなたかもしれませんこれを考慮したい:

re.sub(r'[^\x00-\x7f]', ' ', u'abcdäefgh') 
u'abcd efgh' 
関連する問題