使用Python(stream twitter)将多个JSON文件合并到一个文件中

2024-06-16 11:40:14 发布

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

我从推特上获取了数据。目前,数据位于多个文件中,我无法将其合并到单个文件中

注意:所有文件均为JSON格式

我使用的代码是herehere

It has been suggestedglopto compile JSON files合作

正如我在一些关于使用Python合并JSON的教程中所看到的那样,我编写了这段代码

from glob import glob 
import json
import pandas as pd

with open('Desktop/json/finalmerge.json', 'w') as f: 
    for fname in glob('Desktop/json/*.json'): # Reads all json from the current directory 
        with open(fname) as j: 
            f.write(str(j.read())) 
            f.write('\n') 
            

我成功合并了所有文件,现在该文件是finalmerge.json

现在,我在几个线程中使用了这一建议:


df_lines = pd.read_json('finalmerge.json', lines=True)
df_lines


1000000*23 columns 

Then, what I should do to make each feature in separate columns?



I'm not sure why what's wrong with JSON files, I checked the file that I merge and I found it's not valid as JSON file? what I should do to make this as a data frame?

The reason I am asking this is that I have very basic python knowledge and all the answers to similar questions that I have found are way more complicated than I can understand. Please help this new python user to convert multiple Json fils to one JSON file.

Thank you


Tags: 文件thetoimportjsonthataswith
1条回答
网友
1楼 · 发布于 2024-06-16 11:40:14

我认为问题在于您的文件不是真正的json(或者更好,它们的结构是jsonl)。您有两种处理方法:

  1. 您可以将每个文件作为文本文件读取,然后逐行合并它们
  2. 您可以将它们转换为json(在文件开头添加一个方括号,在每个json元素末尾添加一个逗号)

尝试下面这个问题,并让我知道它是否解决了您的问题:Loading JSONL file as JSON objects

您也可以尝试通过以下方式编辑代码:

with open('finalmerge.json', 'w') as f:
    for fname in glob('Desktop/json/*.json'): 
        with open(fname) as j:
            f.write(str(j.read()))
            f.write('\n')

每一行都是不同的json元素

相关问题 更多 >