我正在做一个情绪分析项目,但我无法解决与json相关的错误

时间:2018-07-22 15:59:24

标签: python

我得到的错误是:

python3 sentim.py afinn.txt  tweet_file
    Traceback (most recent call last):
      File "sentim.py", line 67, in <module>
        main()
      File "sentim.py", line 29, in main
        js = json.loads(str(line))
      File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
        return _default_decoder.decode(s)
      File "/usr/lib/python3.5/json/decoder.py", line 339, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/usr/lib/python3.5/json/decoder.py", line 357, in raw_decode
        raise JSONDecodeError("Expecting value", s, err.value) from None
    json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 1)

这是我的代码,我无法删除与json相关的错误。我的项目是基于afinn.txt

在Twitter上进行的情绪分析
    def main():
        sent_file = open(sys.argv[1])
        tweet_file = open(sys.argv[2])
        #hw()
        #lines(sent_file)
        #lines(tweet_file)

        # Read the sentiment file and build dictionary
        scores = {} # initialize an empty dictionary
        for line in sent_file:
            term, score = line.split("\t") # The file is tab delimited.
            scores[term] = int(score)

        #print scores.items() # Print every (term, score) pair in the dictionary. As a list.

        # Read the tweet file: "output.txt"
        tweet_data = []
        for line in tweet_file:
           js = json.loads(str(line))

        '''if "lang" in response.keys():
                 print response["lang"]'''
        if "text" in response.keys():
                tweet_data.append(response["text"])
                #print response["text"]
            #print response.keys()

        #print len(tweet_data)

        # For each tweet
        for t in tweet_data:
            total = 0
            # Convert from <type 'unicode'> to <type 'str'>
            encoded_t = t.encode('utf-8')

0 个答案:

没有答案