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.
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)
Google may be blocking your IP, use a VPN and it should work.
In my case, the error was caused by too many requests in a short time period and my IP address was temporarily blocked. I tried it the next day again and everything worked well.
In my case, it's caused by emoji in strings. I removed them and everything works well.
This is what I had to do to bypass their API call restriction... I use a VPN, specifically Nord-Vpn, so to do it the way I did you would need to be able to connect/disconnect from/to a VPN through the terminal...
def translate_text(text, dest_language="en"):
# Used to translate using the googletrans library
import json
translator = googletrans.Translator()
try:
translation = translator.translate(text=text, dest=dest_language)
except json.decoder.JSONDecodeError:
# api call restriction
process = subprocess.Popen(["nordvpn", "d"], stdout=subprocess.PIPE)
process.wait()
process = subprocess.Popen(["nordvpn", "c", "canada"], stdout=subprocess.PIPE)
process.wait()
return Process_Data.translate_text(text=text, dest_language=dest_language)
return translation
来源:https://stackoverflow.com/questions/49497391/googletrans-api-error-expecting-value-line-1-column-1-char-0