Python Google Translate API error
Asked Answered
L

2

3

I am very to new to python and trying to translate a bunch of keywords using google API. I have an excel file with 3000 keywords which are mix of english, spanish, german etc. Trying to translate everything to English.

However, every time I run my code, I get error at different values. Sometimes, my code give error at 810th keyword while sometime it gives error at 1038 keyword. And I am not even editing the layout of the file.

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Below is my code:

from googletrans import Translator
import pandas
import math
import time
df = pandas.read_excel(r'Desktop/python_keywords.xlsx')
keywords = df['Keywords']
Translate = []
translator = Translator()

for i in range(0,len(keywords)):
    word = translator.translate(str(keywords[j])).text
    Translate.append(word)
Laryngitis answered 23/2, 2018 at 23:26 Comment(0)
C
3

Normally this error is due to the character limit of 15K in Googletrans API.

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Consider reducing the number of characters.

Clipped answered 27/3, 2018 at 10:49 Comment(1)
Is it tied to some time constraint or in a one single shot ?Demount
L
0

Which line of your code gives you this error? Look at error trace
Let's start with your iterators: it is declared as i, but then you use j. Then check length of your request. It shouldn't be longer than 5k symbols according to JSONDecodeError using Google Translate API with Python3.
Anyway, it looks like api responds with empty json and you have to add at least try .. except to avoid this error, smth like this:

try:  
    word = translator.translate(str(keywords[j])).text  
except JSONDecodeError as err:  
    print(err)  # if you want to see when error happens
else:
    Translate.append(word)
Luzon answered 26/2, 2018 at 3:2 Comment(1)
Thanks Nikita. Yeah the iterator is not an issue. I just mistyped it. I think its the character limit.Laryngitis

© 2022 - 2024 — McMap. All rights reserved.