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)
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
Google may be blocking your IP, use a VPN and it should work.
There could be 2 reasons for this:
1. IP address is temporarily blocked.
2. You have reached the character limit.
I faced the same issue and ended up using another package called translate
and it works flawlessly. The syntax is pretty similar too. You can find it here or do pip install translate
In my case, it's caused by emoji in strings. I removed them and everything works well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With