功能单码
Function unicode
我有一个程序,其中有一个名为 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 但我仍然不知道我应该怎么做才能解决这个问题
有人可以帮助我吗?
s
是 None
:
>>> import unicodedata
>>> unicodedata.normalize('NFD', None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be unicode, not None
检查 stt.stt()
returns 的情况,或处理 None
的情况。
生成器表达式括号是可选的,但是当使用 str.join()
时,实际上在这里使用列表理解更快(str.join()
代码 有 将输入转换为列表,因为它需要遍历列表两次):
def remove_tildes(s):
# Remove spanish tildes so there won't be errors with ascii
return ''.join([
c for c in unicodedata.normalize('NFD', s or u'')
if unicodedata.category(c) != 'Mn'])
其中 s or u''
通过将其替换为空字符串来处理 s
为 None
的情况。
您是否可能将 None 作为参数传递?
尝试:
def remove_tildes(s):
if s:
return ''.join((c for c in unicodedata.normalize('NFD', s)
if unicodedata.category(c) != 'Mn'))
我有一个程序,其中有一个名为 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 但我仍然不知道我应该怎么做才能解决这个问题
有人可以帮助我吗?
s
是 None
:
>>> import unicodedata
>>> unicodedata.normalize('NFD', None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be unicode, not None
检查 stt.stt()
returns 的情况,或处理 None
的情况。
生成器表达式括号是可选的,但是当使用 str.join()
时,实际上在这里使用列表理解更快(str.join()
代码 有 将输入转换为列表,因为它需要遍历列表两次):
def remove_tildes(s):
# Remove spanish tildes so there won't be errors with ascii
return ''.join([
c for c in unicodedata.normalize('NFD', s or u'')
if unicodedata.category(c) != 'Mn'])
其中 s or u''
通过将其替换为空字符串来处理 s
为 None
的情况。
您是否可能将 None 作为参数传递?
尝试:
def remove_tildes(s):
if s:
return ''.join((c for c in unicodedata.normalize('NFD', s)
if unicodedata.category(c) != 'Mn'))