JSONDecodeError: Expecting value: line 1 column 1 (char 0) while translating text

社会主义新天地 提交于 2020-06-27 16:48:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!