试图从包含tweet d的JSON文件中提取某些键

2024-04-27 00:09:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用的代码如下:

import json

json_file = 'stream.txt'
open_file = open(json_file, "r")

for line in open_file:    
    each_tweet = json.loads(line.strip())
    if 'text' in each_tweet:
        print(each_tweet['id'])
        print(each_tweet['text'])

        hashtags = []
        for tag in each_tweet['entities']['hashtags']:
            hashtags.append(tag['text'])
        print(hashtags)

我使用以下JSON文件作为结束完整JSON文件将使用的格式示例(通过twitter API检索的数据):

https://pastebin.com/SKz6Hgvn

但是,我得到以下错误:

File "C:\Anaconda3\lib\json\decoder.py", line 357, in raw_decode
     raise JSONDecodeError("Expecting value", s, err.value) from None

JSONDecodeError: Expecting value

虽然这似乎是一个相当简单的错误,但我似乎找不到补救的方法

任何帮助都将是一个感谢,我在编码相当生疏哈哈


Tags: 文件textinjsonforvaluetagline