简体   繁体   中英

AttributeError: 'NoneType' object has no attribute 'group' while using googletrans library

everything work before but it error now when i try using googletrans library. here my code

import googletrans    
from googletrans import Translator

translator = Translator()
df['data'] = df['tweet'].apply(lambda x: translator.translate(x, dest='en').text)
0 tweet
0 Heureusement que Macron n'a pas profité de la guerre en Ukraine pour être réélu mdr
1 L'Ukraine Ce pays formidable N'est-ce pas Macron le fou
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-1c7cdbf959e6> in <module>
      4 
      5 translator = Translator()
----> 6 df['data'] = df['tweet'].apply(lambda x: translator.translate(x, dest='en').text)
      7 
      8 df['data'].to_csv('Dataset_EN.csv')

~\anaconda3\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwds)
   4136             else:
   4137                 values = self.astype(object)._values
-> 4138                 mapped = lib.map_infer(values, f, convert=convert_dtype)
   4139 
   4140         if len(mapped) and isinstance(mapped[0], Series):

pandas\_libs\lib.pyx in pandas._libs.lib.map_infer()

<ipython-input-4-1c7cdbf959e6> in <lambda>(x)
      4 
      5 translator = Translator()
----> 6 df['data'] = df['tweet'].apply(lambda x: translator.translate(x, dest='en').text)
      7 
      8 df['data'].to_csv('Dataset_EN.csv')

~\anaconda3\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.

~\anaconda3\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)

~\anaconda3\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

~\anaconda3\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'

your code work properly for me. make sure you have the latest googletrans version or 4.0.0-rc1.

here for check your googletrans version

pip freeze

and here for install googletrans version 4.0.0-rc1

install googletrans==4.0.0-rc1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM