我有一个程序,其中有一个函数叫做stt.stt()我是西班牙人,所以我得去掉波浪形文字,把文字翻过来stt.stt()转换为unicode,为此我有以下函数:
def remove_tildes(s):
return ''.join((c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn')) #Remove spanish tildes so there won't be errors with ascii
phrase=remove_tildes(stt.stt())
但是当我运行程序时,我得到了一个错误:
File "./program2.py", line 14, in remove_tildes
return ''.join((c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn')) #Remove spanish tildes so there won't be errors with ascii
TypeError: must be unicode, not None
为了解决这个问题,我尝试了phrase=remove_tildes(basestring(stt.stt(), unicode))
,phrase=remove_tildes(u stt.stt())
和phrase=remove_tildes(unicode stt.stt())
但是什么都不管用,我也读过这个https://docs.python.org/2/library/unicodedata.html但是我仍然不知道该怎么做来修复这个问题
有人能帮我吗?你知道吗
是否有可能传递None作为参数?你知道吗
尝试:
s
是None
:检查
stt.stt()
返回的内容,或者处理None
情况。你知道吗生成器表达式括号是可选的,但是当使用
str.join()
时,在这里使用列表理解实际上更快(代码将输入转换为列表,因为它需要遍历列表两次):其中
s or u''
通过将s
替换为空字符串来处理None
的情况。你知道吗相关问题 更多 >
编程相关推荐