OliveSensorAPI/scripts/merge_json.py

40 lines
1.2 KiB
Python
Raw Normal View History

2024-01-19 15:02:00 +08:00
import json
import os
def save_merge_json(data_lis, file_path):
with open(file_path, 'wt', encoding='utf-8') as file:
2024-01-19 15:52:53 +08:00
json.dump(data_lis, file, indent=4, ensure_ascii=False)
2024-01-19 15:02:00 +08:00
def get_all_file_paths(folder_path):
2024-01-19 15:52:53 +08:00
files = os.listdir(folder_path)
path = []
for file in files:
file_path = os.path.join(folder_path, file)
if os.path.isdir(file_path):
path.extend(get_all_file_paths(file_path))
else:
path.append(file_path)
return path
2024-01-19 15:02:00 +08:00
if __name__ == '__main__':
conversion_lis = []
2024-01-19 15:52:53 +08:00
folder_path = '' # input
merge_path = '' # input
paths = get_all_file_paths(folder_path=folder_path)
2024-01-19 15:02:00 +08:00
2024-01-19 15:52:53 +08:00
for path in paths:
2024-01-19 15:02:00 +08:00
print(path)
2024-01-19 15:52:53 +08:00
with open(path, 'rt', encoding='utf-8') as lines:
for line in lines:
2024-01-19 15:02:00 +08:00
# 移除行尾的换行符
2024-01-19 15:52:53 +08:00
line.rstrip('\n')
2024-01-19 15:02:00 +08:00
# 解析JSON
try:
data = json.loads(line)
conversion_lis.append(data)
except json.JSONDecodeError as e:
print(f"Error decoding JSON: {e}")
2024-01-19 15:52:53 +08:00
save_merge_json(data_lis=conversion_lis, file_path=merge_path)