簡體   English   中英

使用 googletrans python 包進行波斯語的 AttributeError 錯誤

[英]AttributeError error using googletrans python package for farsi language

我正在嘗試使用 googletrans 將英語翻譯成波斯語。 但它給了我這個錯誤

這是我的代碼:

from googletrans import Translator
translator = Translator()
result = translator.translate('This is an egg', dest='fa')

我收到以下錯誤:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
C:\Temp\ipykernel_23736\2714753431.py in <module>
----> 1 result = translator.translate('This is an egg', dest='fa')

c:\programs\python\python37\lib\site-packages\googletrans\client.py in translate(self, text, dest, src, **kwargs)
    180 
    181         origin = text
--> 182         data = self._translate(text, dest, src, kwargs)
    183 
    184         # this code will be updated when the format is changed.

c:\programs\python\python37\lib\site-packages\googletrans\client.py in _translate(self, text, dest, src, override)
     76 
     77     def _translate(self, text, dest, src, override):
---> 78         token = self.token_acquirer.do(text)
     79         params = utils.build_params(query=text, src=src, dest=dest,
     80                                     token=token, override=override)

c:\programs\python\python37\lib\site-packages\googletrans\gtoken.py in do(self, text)
    192 
    193     def do(self, text):
--> 194         self._update()
    195         tk = self.acquire(text)
    196         return tk

c:\programs\python\python37\lib\site-packages\googletrans\gtoken.py in _update(self)
     60 
     61         # this will be the same as python code after stripping out a reserved word 'var'
---> 62         code = self.RE_TKK.search(r.text).group(1).replace('var ', '')
     63         # unescape special ascii characters such like a \x3d(=)
     64         code = code.encode().decode('unicode-escape')

AttributeError: 'NoneType' object has no attribute 'group'

我知道這一定是與“unicode”相關的問題,但我不知道如何解決。

嘗試以下版本:

pip install googletrans==4.0.0-rc1

我已經安裝了它,你的例子工作得很好:

from googletrans import Translator
translator = Translator()
result = translator.translate('This is an egg', dest='fa')
print(result.text)

# این یک تخم مرغ است

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM