pythongoogle-translategoogletrans

GoogleTrans API Error - Expecting value: line 1 column 1 (char 0)


I am having this error when translating thousands of text data in an iteration:

Expecting value: line 1 column 1 (char 0)

My code for translating big amounts of text:

translatedList = []
for index, row in df.iterrows():
    newrow = copy.deepcopy(row)
    try:
        # translate the 'text' column
        translated = translator.translate(row['text'], dest='en')
        newrow['translated'] = translated.text
    except Exception as e:
        print(str(e))
        continue
    translatedList.append(newrow)

I receive this error after translating about 2-3k rows.


Solution

  • I kind of figured out the problem. I think that this is about Google API's request limit.

    I solved this by reinitializing the translator API on every iteration:

    import copy
    from googletrans import Translator
    
    translatedList = []
    for index, row in df.iterrows():
        # REINITIALIZE THE API
        translator = Translator()
        newrow = copy.deepcopy(row)
        try:
            # translate the 'text' column
            translated = translator.translate(row['text'], dest='en')
            newrow['translated'] = translated.text
        except Exception as e:
            print(str(e))
            continue
        translatedList.append(newrow)