Tôi muốn hợp nhất nhiều tệp JSON thành một tệp trong Python. Điều mà tôi muốn làm là nếu có một số tệp .json như:
# temp1.json [{'num':'1', 'item':'smartphone','data':'2019-01-01'}, {'num':'2', 'item':'smartphone','data':'2019-01-02'}, {'num':'3', 'item':'smartphone','data':'2019-01-03'}, {'num':'4', 'item':'smartphone','data':'2019-01-04'}] # temp2.json [{'num':'5', 'item':'smartphone','data':'2019-01-05'}, {'num':'6', 'item':'smartphone','data':'2019-01-06'}, {'num':'7', 'item':'smartphone','data':'2019-01-07'}] # temp3.json [{'num':'8', 'item':'smartphone','data':'2019-01-08'}, {'num':'9', 'item':'smartphone','data':'2019-01-09'}, {'num':'10', 'item':'smartphone','data':'2019-01-10'}, {'num':'11', 'item':'smartphone','data':'2019-01-11'}, {'num':'12', 'item':'smartphone','data':'2019-01-12'}]Các tệp kết quả.json tôi muốn nhận được trông giống như:
# result.json [{'num':'1', 'item':'smartphone','data':'2019-01-01'}, {'num':'2', 'item':'smartphone','data':'2019-01-02'}, {'num':'3', 'item':'smartphone','data':'2019-01-03'}, {'num':'4', 'item':'smartphone','data':'2019-01-04'}, {'num':'5', 'item':'smartphone','data':'2019-01-05'}, {'num':'6', 'item':'smartphone','data':'2019-01-06'}, {'num':'7', 'item':'smartphone','data':'2019-01-07'}, {'num':'8', 'item':'smartphone','data':'2019-01-08'}, {'num':'9', 'item':'smartphone','data':'2019-01-09'}, {'num':'10', 'item':'smartphone','data':'2019-01-10'}, {'num':'11', 'item':'smartphone','data':'2019-01-11'}, {'num':'12', 'item':'smartphone','data':'2019-01-12'}]Kết quả.json các tệp tôi nhận được là:
# result.json [[{'num':'1', 'item':'smartphone','data':'2019-01-01'}, {'num':'2', 'item':'smartphone','data':'2019-01-02'}, {'num':'3', 'item':'smartphone','data':'2019-01-03'}, {'num':'4', 'item':'smartphone','data':'2019-01-04'}], [{'num':'5', 'item':'smartphone','data':'2019-01-05'}, {'num':'6', 'item':'smartphone','data':'2019-01-06'}, {'num':'7', 'item':'smartphone','data':'2019-01-07'}], [{'num':'8', 'item':'smartphone','data':'2019-01-08'}, {'num':'9', 'item':'smartphone','data':'2019-01-09'}, {'num':'10', 'item':'smartphone','data':'2019-01-10'}, {'num':'11', 'item':'smartphone','data':'2019-01-11'}, {'num':'12', 'item':'smartphone','data':'2019-01-12'}]]Tôi đã sử dụng mã để hợp nhất các tệp .json từ đây và thay đổi nó rất giống như bên dưới:
files=['my.json','files.json',...,'name.json'] def merge_JsonFiles(filename): result = list() for f1 in filename: with open(f1, 'r') as infile: result.append(json.load(infile)) with open('counseling3.json', 'w') as output_file: json.dump(result, output_file) merge_JsonFiles(files)Tôi đã đọc một số câu hỏi liên quan, nhưng không có câu trả lời tôi cần. Ai giúp tôi với?
Trong bài viết nhanh này, chúng tôi sẽ tập trung vào một vài ví dụ về cách đọc và hợp nhất nhiều tệp JSON thành một DataFrame của Pandas với Python.how to read and merge multiple JSON files into a single Pandas DataFrame with Python.
Nếu bạn quan tâm đến việc kết hợp các tệp CSV vào DataFrame thì bạn có thể kiểm tra bài viết chi tiết này: Cách hợp nhất nhiều tệp CSV với Python
Mã đầy đủ: Hợp nhất nhiều tệp JSON với Python và Pandas
Bạn có thể tìm mã đầy đủ Cách hợp nhất nhiều tệp JSON với dấu vết của tệp gốc bên dưới:merge multiple JSON files with trace of the origin file below:
import pandas as pd import glob, os, json json_dir = 'data/json_files_dir' json_pattern = os.path.join(json_dir, '*.json') file_list = glob.glob(json_pattern) dfs = [] for file in file_list: with open(file) as f: json_data = pd.json_normalize(json.loads(f.read())) json_data['site'] = file.rsplit("/", 1)[-1] dfs.append(json_data) df = pd.concat(dfs)Nếu bạn thích tìm hiểu thêm về mã này và cách tùy chỉnh nó. Sau đó, bạn có thể kiểm tra các bước tiếp theo:
Bước 1: Liệt kê nhiều tệp JSON trong thư mục
Hợp nhất nhiều tệp yêu cầu một số thư viện Python như: pandas, glob, # result.json [{'num':'1', 'item':'smartphone','data':'2019-01-01'}, {'num':'2', 'item':'smartphone','data':'2019-01-02'}, {'num':'3', 'item':'smartphone','data':'2019-01-03'}, {'num':'4', 'item':'smartphone','data':'2019-01-04'}, {'num':'5', 'item':'smartphone','data':'2019-01-05'}, {'num':'6', 'item':'smartphone','data':'2019-01-06'}, {'num':'7', 'item':'smartphone','data':'2019-01-07'}, {'num':'8', 'item':'smartphone','data':'2019-01-08'}, {'num':'9', 'item':'smartphone','data':'2019-01-09'}, {'num':'10', 'item':'smartphone','data':'2019-01-10'}, {'num':'11', 'item':'smartphone','data':'2019-01-11'}, {'num':'12', 'item':'smartphone','data':'2019-01-12'}] 0 và # result.json [{'num':'1', 'item':'smartphone','data':'2019-01-01'}, {'num':'2', 'item':'smartphone','data':'2019-01-02'}, {'num':'3', 'item':'smartphone','data':'2019-01-03'}, {'num':'4', 'item':'smartphone','data':'2019-01-04'}, {'num':'5', 'item':'smartphone','data':'2019-01-05'}, {'num':'6', 'item':'smartphone','data':'2019-01-06'}, {'num':'7', 'item':'smartphone','data':'2019-01-07'}, {'num':'8', 'item':'smartphone','data':'2019-01-08'}, {'num':'9', 'item':'smartphone','data':'2019-01-09'}, {'num':'10', 'item':'smartphone','data':'2019-01-10'}, {'num':'11', 'item':'smartphone','data':'2019-01-11'}, {'num':'12', 'item':'smartphone','data':'2019-01-12'}] 1.
Tiếp theo, chúng ta có thể thấy cách liệt kê các tệp JSON trong một thư mục có Python:list JSON files in a folder with Python:
import pandas as pd import glob, os, json json_dir = 'data/json_files_dir' json_pattern = os.path.join(json_dir, '*.json') file_list = glob.glob(json_pattern)Điều này sẽ dẫn đến một danh sách với các tệp JSON tuyệt đối như:
['/data/json_files_dir/file1.json', '/data/json_files_dir/file2.json']]
'/data/json_files_dir/file2.json'
]
Lưu ý: Đối với các dòng JSON, bạn có thể cần thay đổi mẫu phù hợp thành # result.json [{'num':'1', 'item':'smartphone','data':'2019-01-01'}, {'num':'2', 'item':'smartphone','data':'2019-01-02'}, {'num':'3', 'item':'smartphone','data':'2019-01-03'}, {'num':'4', 'item':'smartphone','data':'2019-01-04'}, {'num':'5', 'item':'smartphone','data':'2019-01-05'}, {'num':'6', 'item':'smartphone','data':'2019-01-06'}, {'num':'7', 'item':'smartphone','data':'2019-01-07'}, {'num':'8', 'item':'smartphone','data':'2019-01-08'}, {'num':'9', 'item':'smartphone','data':'2019-01-09'}, {'num':'10', 'item':'smartphone','data':'2019-01-10'}, {'num':'11', 'item':'smartphone','data':'2019-01-11'}, {'num':'12', 'item':'smartphone','data':'2019-01-12'}] 2
Bước 2: Đọc và hợp nhất nhiều tệp JSON vào DataFrame
Cuối cùng, chúng tôi sẽ xử lý tất cả các tệp JSON được tìm thấy trong từng bước trước một.process all JSON files found in the previous step one by one.
Chúng tôi đang đọc các tệp với # result.json [{'num':'1', 'item':'smartphone','data':'2019-01-01'}, {'num':'2', 'item':'smartphone','data':'2019-01-02'}, {'num':'3', 'item':'smartphone','data':'2019-01-03'}, {'num':'4', 'item':'smartphone','data':'2019-01-04'}, {'num':'5', 'item':'smartphone','data':'2019-01-05'}, {'num':'6', 'item':'smartphone','data':'2019-01-06'}, {'num':'7', 'item':'smartphone','data':'2019-01-07'}, {'num':'8', 'item':'smartphone','data':'2019-01-08'}, {'num':'9', 'item':'smartphone','data':'2019-01-09'}, {'num':'10', 'item':'smartphone','data':'2019-01-10'}, {'num':'11', 'item':'smartphone','data':'2019-01-11'}, {'num':'12', 'item':'smartphone','data':'2019-01-12'}] 3 và tải chúng dưới dạng bản ghi JSON bằng phương thức # result.json [{'num':'1', 'item':'smartphone','data':'2019-01-01'}, {'num':'2', 'item':'smartphone','data':'2019-01-02'}, {'num':'3', 'item':'smartphone','data':'2019-01-03'}, {'num':'4', 'item':'smartphone','data':'2019-01-04'}, {'num':'5', 'item':'smartphone','data':'2019-01-05'}, {'num':'6', 'item':'smartphone','data':'2019-01-06'}, {'num':'7', 'item':'smartphone','data':'2019-01-07'}, {'num':'8', 'item':'smartphone','data':'2019-01-08'}, {'num':'9', 'item':'smartphone','data':'2019-01-09'}, {'num':'10', 'item':'smartphone','data':'2019-01-10'}, {'num':'11', 'item':'smartphone','data':'2019-01-11'}, {'num':'12', 'item':'smartphone','data':'2019-01-12'}] 4.
Cuối cùng, chúng tôi sẽ tạo ra một bản dữ liệu gấu trúc với # result.json [{'num':'1', 'item':'smartphone','data':'2019-01-01'}, {'num':'2', 'item':'smartphone','data':'2019-01-02'}, {'num':'3', 'item':'smartphone','data':'2019-01-03'}, {'num':'4', 'item':'smartphone','data':'2019-01-04'}, {'num':'5', 'item':'smartphone','data':'2019-01-05'}, {'num':'6', 'item':'smartphone','data':'2019-01-06'}, {'num':'7', 'item':'smartphone','data':'2019-01-07'}, {'num':'8', 'item':'smartphone','data':'2019-01-08'}, {'num':'9', 'item':'smartphone','data':'2019-01-09'}, {'num':'10', 'item':'smartphone','data':'2019-01-10'}, {'num':'11', 'item':'smartphone','data':'2019-01-11'}, {'num':'12', 'item':'smartphone','data':'2019-01-12'}] 5. Tất cả các khung dữ liệu được thêm vào một danh sách.
Bước cuối cùng là kết hợp danh sách các khung dữ liệu thành một danh sách duy nhất bởi: # result.json [{'num':'1', 'item':'smartphone','data':'2019-01-01'}, {'num':'2', 'item':'smartphone','data':'2019-01-02'}, {'num':'3', 'item':'smartphone','data':'2019-01-03'}, {'num':'4', 'item':'smartphone','data':'2019-01-04'}, {'num':'5', 'item':'smartphone','data':'2019-01-05'}, {'num':'6', 'item':'smartphone','data':'2019-01-06'}, {'num':'7', 'item':'smartphone','data':'2019-01-07'}, {'num':'8', 'item':'smartphone','data':'2019-01-08'}, {'num':'9', 'item':'smartphone','data':'2019-01-09'}, {'num':'10', 'item':'smartphone','data':'2019-01-10'}, {'num':'11', 'item':'smartphone','data':'2019-01-11'}, {'num':'12', 'item':'smartphone','data':'2019-01-12'}] 6
dfs = [] for file in file_list: with open(file) as f: json_data = pd.json_normalize(json.loads(f.read())) json_data['site'] = file.rsplit("/", 1)[-1] dfs.append(json_data) df = pd.concat(dfs)Nếu bạn muốn có một dấu vết của mỗi bản ghi mà từ tệp sắp tới - thì bạn có thể sử dụng một dòng như:
json_data['site'] = file.rsplit("/", 1)[-1]Chúng tôi đang chuyển đổi đường dẫn tệp tuyệt đối trong tên tệp vì vậy:
'/data/json_files_dir/file1.json'
sẽ được giữ như:
'file1.json'