json.decoder.JSONDecodeError:期望值:第1行第1列(字符0)(文件写入)

时间:2019-04-05 22:18:45

标签: python

我正在尝试编写一个程序来获取字符串输入文件,然后使用googletrans将其从各种语言翻译成英语。它正在工作,然后随机停止并不断提供错误代码。

起初,我使用utf-8,但不得不切换到utf_16进行编码。我还尝试使用emoji模块删除emoji。

from googletrans import Translator
import emoji
translator = Translator()
tweets = set()
for line in open('tweets.txt', encoding='utf_16'):
    line = emoji.demojize(line)
    print(line)
    line = translator.translate(line).text
    line = emoji.emojize(line)
    print(line)
tweets.add(line)
f = open('tweets_without_repeats.txt', 'w', encoding='utf_16')
for i in tweets:
    f.write(i)
f.close()

在没有表情符号的情况下打印第一行,但在翻译之前

'''File "c:/Users/balle/OneDrive/Documents/IT and Societ Paper/tweet_no_repeats.py", line 8, in <module>  
    line = translator.translate(line).text  
  File "c:\Users\balle\Miniconda3\Lib\site-packages\googletrans\client.py", line 172, in translate  
    data = self._translate(text, dest, src)  
  File "c:\Users\balle\Miniconda3\Lib\site-packages\googletrans\client.py", line 81, in _translate  
    data = utils.format_json(r.text)  
  File "c:\Users\balle\Miniconda3\Lib\site-packages\googletrans\utils.py", line 62, in format_json  
    converted = legacy_format_json(original)  
  File "c:\Users\balle\Miniconda3\Lib\site-packages\googletrans\utils.py", line 54, in legacy_format_json  
    converted = json.loads(text)  
  File "c:\Users\balle\Miniconda3\Lib\json\__init__.py", line 348, in loads  
    return _default_decoder.decode(s)  
  File "c:\Users\balle\Miniconda3\Lib\json\decoder.py", line 337, in decode  
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())  
  File "c:\Users\balle\Miniconda3\Lib\json\decoder.py", line 355, in raw_decode  
    raise JSONDecodeError("Expecting value", s, err.value) from None  
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)  
'''

0 个答案:

没有答案