[英]json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) error occurs sometimes using googletrans and Pandas in Python
I have the following code that sometimes works and sometimes doesn't.. I haven't figured out any patterns as to why the code wouldn't work, but I have confirmed the following:我有以下代码,有时有效,有时无效。我还没有弄清楚为什么代码不起作用的任何模式,但我已经确认了以下内容:
I believe it has something to do with tapping into the Google API, but I do not know.我相信这与利用 Google API 有关,但我不知道。 Does anyone know what the issue is here?
有谁知道这里的问题是什么? If not, are there alternatives out there for language translation in python?
如果没有,python 中的语言翻译是否有替代方案?
Error:错误:
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Code:代码:
from googletrans import Translator
import pandas as pd
import xlsxwriter
import xlrd
import copy
##################TRANSLATION
translator = Translator()
file = r"xxxx"
#dt2 = translator.detect(text2)
df = pd.read_excel(file, sheet_name = 'Sheet1', converters={'Comments':str}).fillna(0)
df = df[df['Comments'] != 0]
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['Comments'], dest='en')
newrow['translated'] = translated.text
except Exception as e:
print(str(e))
continue
translatedList.append(translated.text)
df = df.assign(translatedv4 = translatedList)
#translator = Translator()
#df['English'] = df['Comments'].apply(translator.translate,dest='en').apply(getattr, args=('text',)) #MAY RUN INTO LIMITS
#df['Translated_Python'] = df['Comments'].map(lambda x: translator.translate(x, src="de", dest="en").text)
#print(df['Translated_Python'])
#s = fuzz.ratio("Wow year what a ","This is a test")
#print(s)
end = r"xxxx"
writer = pd.ExcelWriter(end, engine='xlsxwriter')
df.to_excel(writer, sheet_name='Data')
translatedList.to_excel(writer, sheet_name='List')
writer.save()
The issue here is that this library sends an API call to the Google Translate service for every translate call.这里的问题是这个库会为每个翻译调用发送一个 API 调用到谷歌翻译服务。 You're wrapping the translate call in an
apply
, so you're slamming the Translate API with a call for every row.您将 translate 调用包装在
apply
中,因此您正在猛烈抨击 Translate API 并调用每一行。 The library expects to get back JSON, but when it gets back an error from Google, like 429 Too Many Requests , this can't be parsed into JSON .该库希望返回 JSON,但是当它从 Google 返回错误时,例如429 Too Many Requests ,无法将其解析为 JSON 。
If you want to do bulk translation, you need to use the libraries recommended approach:如果要进行批量翻译,则需要使用库推荐的方法:
https://github.com/ssut/py-googletrans#advanced-usage-bulk https://github.com/ssut/py-googletrans#advanced-usage-bulk
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.