python - 从 None 获取 "raise JSONDecodeError("Expecting value", s, err.value) json.decoder.JSONDecodeError

标签 python json python-3.x jsondecoder

当我尝试运行以下代码时,出现 JSONDecodeError 错误。我不知道如何解决它。

我正在尝试流式传输推文。对于几条推文来说它工作正常,突然我收到错误并且流停止,请帮助我如何解决这个问题? 更新的完整代码

from tweepy import API
from tweepy import Cursor
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from textblob import TextBlob
from googletrans import Translator
import json
import re
import csv

ACCESS_TOKEN = "2446723783-cqGTmWPdtJI5HhCT7UmmET2xVY7xlA1RqlBQdQl"
ACCESS_TOKEN_SECRET = "6ZRIqd9HDCgZxsR6iMUbt6eOgk1YVsz59lEsvL30eFnPf"
CONSUMER_KEY = "slWhR4Z6VOjp0R3ojx1B2a4pr"
CONSUMER_SECRET = "6OrySqqUIwQM8ioBycqbStWizM6KkLCMFecjXPvbVmsrsw6eNT"

Count = 0


translator = Translator()
class StdOutListener(StreamListener):

    def on_data(self, data):
        global Count
        #Data has the JSON STring
        y = json.loads(data)

        print("--------------------")
        try:
            y = y['extended_tweet']['full_text']
            y = str(y)
            ## Portuguese to English conversion block
            translations = translator.translate(y, dest='en')
            print(translations.origin, '\n -> \n', translations.text)
            z = translations.text
            a = translations.origin
            row = [data,a,z]
            with open(fetched_tweets_filename, 'a') as csvFile:
                writer = csv.writer(csvFile)
                writer.writerow(row)

            Count = Count + 1
            print(Count)
        except KeyError:
            y = y['text']
            y = str(y)
            ## Portuguese to English conversion block
            translations = translator.translate(y, dest='en')
            print(translations.origin, '\n -> \n', translations.text)
            z = translations.text
            a = translations.origin
            row = [data,a,z]
            with open(fetched_tweets_filename, 'a') as csvFile:
                writer = csv.writer(csvFile)
                writer.writerow(row)

            Count = Count + 1
            print(Count)

        ### Sentiment Analysis block    
        analysis = TextBlob(z)

        if analysis.sentiment.polarity > 0:
            print("sentiment is positiv")
        elif analysis.sentiment.polarity == 0:
            print("sentiment is Neutral")
        else:
            print("sentiment is Negative")
        print("--------------------\n")    

        return True

    def on_error(self,status):
        print(status)


if __name__ == "__main__":
    listener=StdOutListener()
    auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

    fetched_tweets_filename = "datas3.csv"

    stream = Stream(auth, listener)
    stream.filter(track=['como'],languages=['pt'])



引文

--------------------
RT @VBrasil_: Você decidiu chamar todos os meninos para assistir um filme na sua casa! Mas como cada um queria assistir algo, vocês decidir… 
 -> 
 RT @VBrasil_: You decided to call all the boys to watch a movie in your house! But as everyone wanted to watch something, you decide ...
1
sentiment is Neutral
--------------------

--------------------
RT @queroqtefodas2: Como é ter uma turma unida ?  
 -> 
 RT @myselfs: What is it like to have a group together? 
2
sentiment is Neutral
--------------------

--------------------
RT @thiegonovais: FUTURO AMEAÇADO?

Entenda como é importante os fãs de Harry Potter se unirem mais do que nunca. Isso pode impactar tudo!… 
 -> 
 RT @thiegonovais: FUTURE THREATENED?

Understand how important it is for Harry Potter fans to come together more than ever. This can impact everything! ...
3
sentiment is positiv
--------------------

--------------------
RT @nadiardgs: como assim você OUSA seguir no Instagram a conta do meu namorado de 27 anos desempregado streamer de lol e fã de Djonga?
tá… 
 -> 
 RT @nadiardgs: how dare you follow on Instagram the account of my unemployed 27-year-old boyfriend lol streamer and Djonga fan?
OK…
4
sentiment is positiv
--------------------

--------------------
Amanda foi dormir na amiga dela, e eu fiquei sozinha como sempre. 
 -> 
 Amanda went to bed with her friend, and I was alone as usual.
5
sentiment is Negative
--------------------

--------------------
Este é só um exemplo de "voto de cabresto" pelo interior do nosso Brasil. O @ptbrasil sabia fazer isto muito bem, entretanto... 
 -> 
 This is just an example of a "halting vote" for the interior of our Brazil. @Ptbrasil knew how to do this very well, though ...
6
sentiment is positiv
--------------------

--------------------
E ainda tem gente que tira sarro de feminismo vtnc... 
Com nossa luta há décadas ainda vemos notícias como essa diariamente, imagina sem! 

 -> 
 And there are still people who make fun of vtnc feminism ...
With our struggle for decades we still see news like this every day, imagine without!

7
sentiment is positiv
--------------------

--------------------
RT @cvrlosmdz: Eu nem tenho palavras para quem quebra pinky promise smh como é que conseguem quebrar uma cena tão séria ? Oh pinky mm é a p… 
 -> 
 RT @cvrlosmdz: I do not even have words for anyone who breaks pinky promise smh how can they break such a serious scene? Oh pinky mm is the p ...
8
sentiment is Negative
--------------------

--------------------
Se tem uma coisa que vou sentir saudade quando defender o mestrado é a refeição por 3 reais no RU, putz como eu amo pagar só treisss reaissssss numa refeição

QUANTO?

SÓ TREISSSSS REAISSSSS 
 -> 
 If there is one thing I will miss when defending the masters is the meal for 3 reals in the UK, putz as I love to pay only real reals in a meal

HOW MUCH?

ONLY TREISSSSS REAISSSSS
9
sentiment is positiv
--------------------

--------------------
Estou doida p/ir pro cinema, porem cm o gato mas como eu não tenho kkk vai cm as manas mesmo!!!! 
 -> 
 I'm crazy to go to the cinema, but the cat but I do not have kkk goes in the same manas !!!!
10
sentiment is Negative
--------------------

--------------------
#expedientefutebol futebol é assim, cabe ao jogador se portar como exemplo que é, e enquanto todos lamberem ele, irá continuar assim, e na seleção brasileira ele joga merda nenhuma 
 -> 
 # football football is so, it's up to the player to be an example, and while everyone licks it, will continue like this, and in the Brazilian team he plays no shit
11
sentiment is positiv
--------------------

--------------------
KKKKKKKKKKK, nem como tanto assim 
 -> 
 KKKKKKKKKKK, or how much
12
sentiment is positiv
--------------------

--------------------
Tô assistindo os vídeos antigos do Felipe Neto e lembrando de como eu era feliz 
 -> 
 I'm watching Felipe Neto's old videos and remembering how happy I was
13
sentiment is positiv
--------------------

--------------------
a julia as vezes eh tao seca que qnd fala eu te amo eu fico boba 
 -> 
 Julia sometimes it's so dry that you say I love you, I'm silly.
14
sentiment is Negative
--------------------

--------------------
RT @hellboyahs: gente me ajuda, como dizer para uma pessoa que não quer mais conversar com ela por ela ser chata mas sem magoar a pessoa? 
 -> 
 RT @hellboyahs: People help me, how to tell a person who does not want to talk to her anymore because she is annoying but not hurting the person?
15
sentiment is Negative
--------------------

--------------------
@resistxre @CabelloJane458 Uai cada um tem sua opinião e tem pessoas que expressão sua opinião como vc expressou a sua . O RBR é uma produção tosca kkkkkkkkkkk da vergonha alheia pq poderia ser melhor a Record poderia fazer algo legal se não nem fizesse pra virar aquilo . 
 -> 
 @resistxre @ CabelloJane458 Uai everyone has their opinion and has people who express their opinion as you expressed yours. The RBR is a crude production kkkkkkkkkkk of the shame of others could be better Record could do something cool if it did not even make it turn.
16
sentiment is positiv
--------------------

--------------------
RT @Bultaourune: Lembando que o BTS é o #1 Ato Coreano >NA HISTÓRIA< indicado a Top Duo/Group no BBMAs, also eles serão tb o #1 k-Ato a gan… 
 -> 
 RT @Bultaourune: Remembering that the BTS is the # 1 Korean Act & NA HISTORY & lt; indicated the Top Duo / Group in the BBMAs, also they will be tb o # 1 k-Ato a gan ...
17
sentiment is positiv
--------------------

--------------------
Traceback (most recent call last):
  File "C:\Users\ManoharRaoN\Desktop\SentizAnalyzer\Version3.py", line 85, in <module>
    stream.filter(track=['como'],languages=['pt'])
  File "C:\Users\ManoharRaoN\AppData\Local\Programs\Python\Python37\lib\site-packages\tweepy\streaming.py", line 453, in filter
    self._start(is_async)
  File "C:\Users\ManoharRaoN\AppData\Local\Programs\Python\Python37\lib\site-packages\tweepy\streaming.py", line 368, in _start
    self._run()
  File "C:\Users\ManoharRaoN\AppData\Local\Programs\Python\Python37\lib\site-packages\tweepy\streaming.py", line 300, in _run
    six.reraise(*exc_info)
  File "C:\Users\ManoharRaoN\AppData\Local\Programs\Python\Python37\lib\site-packages\six.py", line 693, in reraise
    raise value
  File "C:\Users\ManoharRaoN\AppData\Local\Programs\Python\Python37\lib\site-packages\tweepy\streaming.py", line 269, in _run
    self._read_loop(resp)
  File "C:\Users\ManoharRaoN\AppData\Local\Programs\Python\Python37\lib\site-packages\tweepy\streaming.py", line 331, in _read_loop
    self._data(next_status_obj)
  File "C:\Users\ManoharRaoN\AppData\Local\Programs\Python\Python37\lib\site-packages\tweepy\streaming.py", line 303, in _data
    if self.listener.on_data(data) is False:
  File "C:\Users\ManoharRaoN\Desktop\SentizAnalyzer\Version3.py", line 48, in on_data
    translations = translator.translate(y, dest='en')
  File "C:\Users\ManoharRaoN\AppData\Local\Programs\Python\Python37\lib\site-packages\googletrans\client.py", line 172, in translate
    data = self._translate(text, dest, src)
  File "C:\Users\ManoharRaoN\AppData\Local\Programs\Python\Python37\lib\site-packages\googletrans\client.py", line 81, in _translate
    data = utils.format_json(r.text)
  File "C:\Users\ManoharRaoN\AppData\Local\Programs\Python\Python37\lib\site-packages\googletrans\utils.py", line 62, in format_json
    converted = legacy_format_json(original)
  File "C:\Users\ManoharRaoN\AppData\Local\Programs\Python\Python37\lib\site-packages\googletrans\utils.py", line 54, in legacy_format_json
    converted = json.loads(text)
  File "C:\Users\ManoharRaoN\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\Users\ManoharRaoN\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\ManoharRaoN\AppData\Local\Programs\Python\Python37\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)
[Finished in 13.7s with exit code 1]
[shell_cmd: python -u "C:\Users\ManoharRaoN\Desktop\SentizAnalyzer\Version3.py"]
[dir: C:\Users\ManoharRaoN\Desktop\SentizAnalyzer]
[path: C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\Webex\Webex\Applications;C:\Program Files\IBM\SPSS\Modeler\18.1.1\ext\bin\spss.TMWBServer\bin;C:\Program Files (x86)\Sennheiser\SoftphoneSDK\;C:\Users\ManoharRaoN\AppData\Local\Programs\Python\Python37;C:\Users\ManoharRaoN\AppData\Local\Programs\Python\Python37\Scripts;C:\Users\ManoharRaoN\AppData\Roaming\nltk_data;C:\Users\ManoharRaoN\AppData\Local\Microsoft\WindowsApps;C:\Users\ManoharRaoN\AppData\Local\Box\Box Edit\]

我认为它发生在某些推文上,当它发生时我该如何传递它。 不确定我是否完全错误。

当我尝试编写包含特殊字符(例如“te amo ❤️”)的字符串时,我也会收到以下错误 下面是错误

--------------------
RT @PortalMessi: “Messi seria para mim, como um jogador que eu vi jogar, o número 1. Meu pai sempre disse que o melhor foi Pelé, mas eu não… 
 -> 
 RT @PortalMessi: "Messi would be for me, as a player I saw play, the number 1. My father always said that the best was Pele, but I did not ...
1
sentiment is positiv
--------------------

--------------------
RT @felipeneto: Informo que a dívida de 170 mil reais do Botafogo comigo como pessoa física foi integralmente perdoada, junto com os juros… 
 -> 
 RT @felipeneto: I report that Botafogo's debt of 170,000 reais with me as a natural person has been totally forgiven, along with interest ...
2
sentiment is positiv
--------------------

--------------------
@MaluMonteiro08 óh @deus como pode tamanha beleza????? 
 -> 
 @ MaluMonteiro08 ohh @deus how can such beauty ?????
3
sentiment is Neutral
--------------------

--------------------
Meu pai amava. 
Descanse em paz, rainha! ❤️ 
 -> 
 My father loved it.
Rest in peace, queen! (I.e.
Traceback (most recent call last):
  File "C:\Users\Stramzik\Desktop\SentizAnalyzer\Version3.py", line 32, in on_data
    y = y['extended_tweet']['full_text']
KeyError: 'extended_tweet'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Stramzik\Desktop\SentizAnalyzer\Version3.py", line 84, in <module>
    stream.filter(track=['como'],languages=['pt'])
  File "C:\Users\Stramzik\AppData\Local\Programs\Python\Python37\lib\site-packages\tweepy\streaming.py", line 453, in filter
    self._start(is_async)
  File "C:\Users\Stramzik\AppData\Local\Programs\Python\Python37\lib\site-packages\tweepy\streaming.py", line 368, in _start
    self._run()
  File "C:\Users\Stramzik\AppData\Local\Programs\Python\Python37\lib\site-packages\tweepy\streaming.py", line 300, in _run
    six.reraise(*exc_info)
  File "C:\Users\Stramzik\AppData\Local\Programs\Python\Python37\lib\site-packages\six.py", line 693, in reraise
    raise value
  File "C:\Users\Stramzik\AppData\Local\Programs\Python\Python37\lib\site-packages\tweepy\streaming.py", line 269, in _run
    self._read_loop(resp)
  File "C:\Users\Stramzik\AppData\Local\Programs\Python\Python37\lib\site-packages\tweepy\streaming.py", line 331, in _read_loop
    self._data(next_status_obj)
  File "C:\Users\Stramzik\AppData\Local\Programs\Python\Python37\lib\site-packages\tweepy\streaming.py", line 303, in _data
    if self.listener.on_data(data) is False:
  File "C:\Users\Stramzik\Desktop\SentizAnalyzer\Version3.py", line 55, in on_data
    writer.writerow(row)
  File "C:\Users\Stramzik\AppData\Local\Programs\Python\Python37\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 5967-5968: character maps to <undefined>
[Finished in 5.5s with exit code 1]
[shell_cmd: python -u "C:\Users\Stramzik\Desktop\SentizAnalyzer\Version3.py"]
[dir: C:\Users\Stramzik\Desktop\SentizAnalyzer]
[path: C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\Webex\Webex\Applications;C:\Program Files\IBM\SPSS\Modeler\18.1.1\ext\bin\spss.TMWBServer\bin;C:\Program Files (x86)\Sennheiser\SoftphoneSDK\;C:\Users\Stramzik\AppData\Local\Programs\Python\Python37;C:\Users\Stramzik\AppData\Local\Programs\Python\Python37\Scripts;C:\Users\Stramzik\AppData\Roaming\nltk_data;C:\Users\Stramzik\AppData\Local\Microsoft\WindowsApps;C:\Users\Stramzik\AppData\Local\Box\Box Edit\]

最佳答案

  1. 检查您获取的数据是否确实是 json,如果出现某些错误,您可能无法获取有效的 json 数据,并尝试将 json.loads() 放入 try/catch block 中。
  2. 在使用正确的编码写入文件时将数据编码为字节。
import json
import csv


class StdOutListener(StreamListener):

    def on_data(self, data):
        global Count
        #Data has the JSON STring


        print("--------------------")
        try:
            y = json.loads(data)
            y = y['extended_tweet']['full_text']
            y = str(y)
            ## Portuguese to English conversion block
            translations = translator.translate(y, dest='en')
            print(translations.origin, '\n -> \n', translations.text)
            z = translations.text.encode("utf-8")
            a = translations.origin.encode("utf-8")
            data = data.encode("utf-8")
            row = [data,a,z]
            with open(fetched_tweets_filename, 'a') as csvFile:
                writer = csv.writer(csvFile)
                writer.writerow(row)

            Count = Count + 1
            print(Count)
        except KeyError:
            y = y.get('text', '')   # change this to get
            y = str(y)
            ## Portuguese to English conversion block
            translations = translator.translate(y, dest='en')
            print(translations.origin, '\n -> \n', translations.text)
            z = translations.text.encode("utf-8")
            a = translations.origin.encode("utf-8")
            row = [data, a, z]
            with open(fetched_tweets_filename, 'a') as csvFile:
                writer = csv.writer(csvFile)
                writer.writerow(row)

            Count = Count + 1
            print(Count)
        except Exception as e:
            print(str(e))

        ### Sentiment Analysis block
        analysis = TextBlob(z)

        if analysis.sentiment.polarity > 0:
            print("sentiment is positiv")
        elif analysis.sentiment.polarity == 0:
            print("sentiment is Neutral")
        else:
            print("sentiment is Negative")
        print("--------------------\n")

        return True

    def on_error(self,status):
        print(status)


if __name__ == "__main__":
    listener=StdOutListener()
    auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

    fetched_tweets_filename = "datas3.csv"

    stream = Stream(auth, listener)
    stream.filter(track=['como'],languages=['pt'])

关于python - 从 None 获取 "raise JSONDecodeError("Expecting value", s, err.value) json.decoder.JSONDecodeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55911678/

相关文章:

python - 如何将 pandas 数据框列的值设置为列

python - 在重构我的 python 脚本时需要帮助

python - isinstance - Python 类

json - Angular 5,rxjs 映射到 json,类型“对象”上不存在 'json'

python - pyspark 如果列具有相同的值,如何组合列值

尝试安装 gevent-socketio 时找不到 python.h

javascript - 使用来自 API 的 JSON 填充数据表

javascript - 如何将带有 getter/setter 的 TypeScript 类实例转换为 JSON 以存储在 MySQL 数据库中

python - 在 Python 中创建不可变对象(immutable对象)

python-3.x - 如何修改 Python 中的列表?