【问题标题】:Writing JSON files in one txt file with each JSON data on its own line将 JSON 文件写入一个 txt 文件,每个 JSON 数据占一行
【发布时间】:2019-05-23 19:31:06
【问题描述】:

所以我正在使用推文 ID 列表查询 Twitter API。我需要做的是遍历 ID,以便从 Twitter 获取相应的数据。然后我需要将这些 JSON 文件存储到一个 txt 文件中,其中每条推文的 JSON 数据都在自己的行中。稍后我将不得不逐行读取 txt 文件以从中创建 pandas df。

我试着给你一些假数据来告诉你结构。

twt.tweet_id.head()

0    000000000000000001
1    000000000000000002
2    000000000000000003
3    000000000000000004
4    000000000000000005
Name: tweet_id, dtype: int64

我不知道如何共享 JSON 文件,我什至不知道我是否可以。调用 tweet._json 后,我得到的是一个 JSON 文件。

drop_lst = []     # this is needed to collect the IDs which don't work


for i in twt.tweet_id:   # twt.tweet_id is the pd.series with the IDs
    try:
        tweet = api.get_status(i)
        with open('tweet_json.txt', 'a') as f:
            f.write(str(tweet._json)+'\n')  #  tweet._json is the JSON file I need

    except tp.TweepError:
        drop_lst.append(i)

上述方法有效,但我认为我丢失了稍后创建数据帧所需的 JSON 结构

drop_lst = []

for i in twt.tweet_id:
    try:
        tweet = api.get_status(i)
        with open('data.txt', 'a') as outfile:  
            json.dump(tweet._json, outfile)

    except tp.TweepError:
        drop_lst.append(i)

上面没有把每个文件放在自己的行上。

我希望我能够为您提供足够的信息来帮助我。

提前感谢您的所有帮助。

【问题讨论】:

    标签: python json pandas


    【解决方案1】:

    使用json.dumpjson 附加到文件中不包含换行符,因此它们都在同一行结束。我建议将所有 json 记录收集到 list,然后使用 join 并将其转储到文件中

    tweets, drop_lst = [], []
    
    for i in twt.tweet_id:
        try:
            tweet = api.get_status(i)
            tweets.append(tweet._json)
    
        except tp.TweepError:
            drop_lst.append(i)
    
    with open('data.txt', 'a') as fh:
        fh.write('\n') # to ensure that the json is on its own line to start
        fh.write('\n'.join(json.dumps(tweet) for tweet in tweets)) # this will concatenate the tweets into a newline delimited string
    

    然后,要创建数据框,您可以读取该文件并将所有内容重新拼接在一起

    with open("data.txt") as fh:
        tweets = [json.loads(line) for line in fh if line]
    
    df = pd.DataFrame(tweets)
    

    这假设json 本身没有换行符,推文可能包含换行符,所以要小心

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-24
      • 2011-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多