1
import pandas as pd
from googletrans import Translator

d = {"City_trad_chinese":["香港特别行政区",
                      "澳门特别行政区",
                      "北京市",
                      "上海市"]}
df = pd.DataFrame(data=d)

translator = Translator()

df["City_English"] = df["City_trad_chinese"].map(lambda x: translator.translate(x, src="zh-TW", dest="en").text)

I've got this Code snippet from this StackOverflow_link:

I want to create this after code snippet:

print(df["City_English"])

0    Hong Kong Special Administrative Region
1        Macao Special Administrative Region
2                               Beijing City
3                              Shanghai City

But when I try it, I'll get the same error:

Traceback (most recent call last):

  File "<ipython-input-30-d5e66e13e679>", line 1, in <module>
df["City_English"] = df["City_trad_chinese"].map(lambda x: translator.translate(x, src="zh-TW", dest="en").text)

  File "C:\Users\xxxx\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\series.py", line 2996, in map
arg, na_action=na_action)

  File "C:\Users\xxxx\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\base.py", line 1004, in _map_values
new_values = map_f(values, mapper)

  File "pandas/_libs/src\inference.pyx", line 1472, in pandas._libs.lib.map_infer

  File "<ipython-input-30-d5e66e13e679>", line 1, in <lambda>
df["City_English"] = df["City_trad_chinese"].map(lambda x: translator.translate(x, src="zh-TW", dest="en").text)

  File "C:\Users\xxxx\AppData\Local\Continuum\anaconda3\lib\site-packages\googletrans\client.py", line 172, in translate
data = self._translate(text, dest, src)

  File "C:\Users\xxxx\AppData\Local\Continuum\anaconda3\lib\site-packages\googletrans\client.py", line 75, in _translate
token = self.token_acquirer.do(text)

  File "C:\Users\xxxx\AppData\Local\Continuum\anaconda3\lib\site-packages\googletrans\gtoken.py", line 180, in do
self._update()

  File "C:\Users\xxxx\AppData\Local\Continuum\anaconda3\lib\site-packages\googletrans\gtoken.py", line 59, in _update
code = unicode(self.RE_TKK.search(r.text).group(1)).replace('var ', '')

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

How can I resolve to the above error?


[NOTE]:

System information:

  • Windows10
  • Anaconda package
  • Spyder IDE
Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
Matt Ross
  • 11
  • 1
  • 1
    The compiler already tells you where the error is: ``code = unicode(self.RE_TKK.search(r.text).group(1)).replace('var ', '')`` - it is telling you that ``self.RE_TKK.search(r.text)`` returns ``None``. – Mike Scotty Oct 11 '18 at 08:14
  • This is probably not the cause of the error, but the Chinese in your post is actually Simplified, not Traditional. – Zilong Li Oct 11 '18 at 08:18
  • But in [link](https://py-googletrans.readthedocs.io/en/latest/) are also Examples: `from googletrans import Translator` `translator = Translator()` `translator.translate('안녕하세요.')` I'll get also the same ErrorMessage Other Languages I've tried also: `translator.translate('veritas lux mea', src='la')` get's the same ErrorMessage also: **AttributeError: 'NoneType' object has no attribute 'group'** – Matt Ross Oct 11 '18 at 08:26
  • It looks like something has changed in `googletrans` since that answer was posted so I don't think the code is wrong but it's a bug in the latest release as I get the same error with googletrans 2.3.0 and running the example code on the website. https://github.com/ssut/py-googletrans/issues/88 there is a workaround here: https://stackoverflow.com/questions/52455774/googletrans-stopped-working-with-error-nonetype-object-has-no-attribute-group/52487148#52487148 so I'd try editing gtoken.py with the answer in the link – EdChum Oct 11 '18 at 09:04
  • I've found the Workaround also but it is also incompatible for me... If I use it, I'll get: `AttributeError: 'TokenAcquirer' object has no attribute 'RE_RAWTKK'` – Matt Ross Oct 11 '18 at 09:26

0 Answers0