问题
I am getting the following error while translating a column from spanish to English:
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
My data frame looks like the following:
case_id es fr
1234 - -
2345 Hola como estas? Encantada de conocerte comment vas-tu aujourd'hui
3456 Hola como estas? Encantada de conocerte -
123321 - comment vas-tu aujourd'hui
'-' is something that shows that there are no comments. My data frame has a blank strings as well apart from comments so I have replaced the blanks with a '-'
I am using the following code:
import googletrans
from googletrans import Translator
translator = Translator()
df['es_en'] = df['es'].apply(lambda x: translator.translate(x, src='es',dest='en').text)
df['fr_en'] = df['fr'].apply(lambda x: translator.translate(x, src='fr',dest='en').text)
What is wrong here? Why I am getting this error?
回答1:
It seems some data related problem, one idea is return NaN
or what need if parsing failed:
def trans(x, s):
try:
return translator.translate(x, src=s, dest='en').text
except:
return np.nan
df['es_en'] = df['es'].apply(lambda x: trans(x, 'es'))
来源:https://stackoverflow.com/questions/62406660/jsondecodeerror-expecting-value-line-1-column-1-char-0-while-translating-tex