brygid
brygid

Reputation: 85

AttributeError: 'NoneType' object has no attribute 'group' from Google translate API using pandas dataframe

I have a dataframe with several columns including a text column, when I use the code from here which is:

import pandas as pd
from googletrans import Translator

translator = Translator(service_urls=['translate.google.com',])

df["translate_result"] = df["to_translate"].apply(lambda x: translator.translate(x, src= "en", dest = "fr").text)

I get the error message :

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

I tried to update the translator function with service urls but it seems not the problem. What could be the issue here? thank you.

Upvotes: 1

Views: 1760

Answers (1)

Ricco D
Ricco D

Reputation: 7287

I got the same error when I used googletrans v3 then I found an open github issue for this error. The suggested fix is to use version googletrans-4.0.0rc1-py3.9.egg-info.

pip install 'googletrans==4.0.0rc1'

translate.py:

from googletrans import Translator
import pandas as pd

translator = Translator()

df = pd.DataFrame({'Spanish':['piso','cama']})
df['English'] = df['Spanish'].apply(translator.translate, src='es', dest='en').apply(getattr, args=('text',))
print(df)

I ran the code from the link you provided in the question and got the expected results.

Test done: enter image description here

enter image description here

Upvotes: 2

Related Questions