我使用 TextBlob 运行语言翻译器。它可以从字符串翻译。但是,我尝试循环数据帧中数据的 textblob 转换器,该数据帧中可能混合了不同的语言(en 和 es(。
我使用的代码是:
for content in data:
blob = TextBlob(content)
for i in data:
blob = TextBlob(i)
blob.translate(from_lang = 'en', to = 'es')
错误是:
83 result = result.encode('utf-8')
84 if result.strip() == source.strip():
---> 85 raise NotTranslated('Translation API returned the input string unchanged.')
86
87 def _request(self, url, host=None, type_=None, data=None):
NotTranslated: Translation API returned the input string unchanged.
因为在每种情况下,"en"和"es"不必不同。在许多情况下,"es"和"en"具有相同的文本。因此,在两者相同的情况下会引发错误。使用 try 和 catch 语句将解决所有具有相同文本的情况,最终使您的代码正常工作。
for content in data:
blob = TextBlob(content)
for i in data:
blob = TextBlob(i)
try:
print (blob.translate(from_lang = 'en', to = 'es'))
except:
print ("Same translation so skipping")