简体   繁体   English

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) 有时在 Python 中使用 googletrans 和 Pandas 会发生错误

[英]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:我有以下代码,有时有效,有时无效。我还没有弄清楚为什么代码不起作用的任何模式,但我已经确认了以下内容:

  • The background data has ~1000 rows背景数据有 ~1000 行
  • All values in df['Comments'] are NOT null df['Comments'] 中的所有值都不是 null

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()

Traceback:追溯: 在此处输入图像描述

Option 2:选项 2: 在此处输入图像描述

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.

相关问题 json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0)python - json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) python Python:json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0) - Python: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) json.decoder.JSONDecodeError:期望值:第2行第1列(char 1)错误 - json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 1) error 错误:json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0) - ERROR: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) Python JSON解析器错误:json.decoder.JSONDecodeError:预期值:第1行第1列(字符0) - Python JSON parser error: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) 为什么python JSON模块显示错误:json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) - Why does the python JSON module show the error : json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) Python~JSON 错误 → json.decoder.JSONDecodeError:预期值:第 1 行第 1 列(字符 0) - Python~JSON error → json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) Python 中的 JSON 错误:json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) - JSON in Python error: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) 从列表中循环时出现 python 错误 json.decoder.JSONDecodeError:预期值:第 1 行第 1 列(字符 0) - python error while looping from a list json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) python中的'json.decoder.JSONDecodeError:预期值:第3行第229列(字符676)'错误 - ‘json.decoder.JSONDecodeError: Expecting value: line 3 column 229 (char 676)’ error in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM