Python TypeError:字符映射必须返回整数、无或unicode

Python TypeError:字符映射必须返回整数、无或unicode,python,unicode,typeerror,python-unicode,Python,Unicode,Typeerror,Python Unicode,我无法弄清楚这个错误是什么以及如何修复它 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

我无法弄清楚这个错误是什么以及如何修复它

    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('ascii')或ord()解决


我是python的业余爱好者,试图从示例代码中学习。我是从一个朋友那里看到的。请有人解释一下问题的根源以及我该如何解决它。谢谢。

您的
文档是
unicode
,而不是
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.
像这样的字典很好:

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

对于您只想用ASCII 127替换所有字符的情况,您可能需要考虑以下事项:

re.sub(r'[^\x00-\x7f]', ' ', u'abcdäefgh')
u'abcd efgh'

您的
文档是
unicode
,而不是
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.
像这样的字典很好:

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

对于您只想用ASCII 127替换所有字符的情况,您可能需要考虑以下事项:

re.sub(r'[^\x00-\x7f]', ' ', u'abcdäefgh')
u'abcd efgh'

它在哪一行抛出错误?这里:text=[[document.translate(trans_table)中逐字逐句。lower().split()]对于live_text中的文档]您的Python版本是什么?Python 3.0到3.3不允许翻译表使用字符串,但需要一个映射。将Python 2.7与AnacondaAt一起使用会抛出错误的行是什么?这里:text=[[document.translate(translate(trans_table).lower().split()]for document in live_text]您的Python版本是什么?Python 3.0到3.3不允许翻译表使用字符串,但需要映射