0

我正在使用 twitterAPI 收集特定关键字的推文。我已经将所有推文收集到一个 json 文件中。数据看起来像这样:

{
    "0": "this is tweet 1",
    "1": "this is tweet 2",
.
.
.
continued
}

现在我想做一个从推文中加载数据的函数。到目前为止,我只成功了这么多:

def read_file(jsonfile):

    #reads from my file and saves it in a list called data

    with open(filename, 'r') as fp:
        #i am not sure what to add here to save it as a list

    data = json.loads(content)
    return data

如果有人能提供帮助,我将不胜感激。

编辑:我希望我的结果是一个名为“xyz”的列表

4

1 回答 1

1

这应该有效:

with open(filename, "r") as fp:
    data = json.load(fp)

您想使用json.load()直接从文件中读取它。Not json.loads()which 从字符串中读取它,这将要求您不必要地首先将其从文件加载到字符串中。

您可以在此处找到有关 json 模块的更多详细信息。

于 2020-05-20T04:42:08.260 回答