add some scripts.
This commit is contained in:
@ -27,6 +27,7 @@ Modification History:
|
||||
import json
|
||||
import csv
|
||||
import os
|
||||
import argparse
|
||||
from collections import defaultdict
|
||||
|
||||
# 读取文件并返回内容
|
||||
@ -76,24 +77,41 @@ def save_to_csv(data, output_file):
|
||||
|
||||
# 主函数,执行数据处理并保存
|
||||
def main():
|
||||
# 使用 argparse 获取命令行参数
|
||||
parser = argparse.ArgumentParser(description="合并多个 JSON 文件并输出到一个新的 JSON 和 CSV 文件")
|
||||
parser.add_argument('files', nargs='+', choices=['birth', 'astro', 'ethnic'],
|
||||
help="指定需要合并的文件, 至少两个, 最多三个: birth, astro, ethnic")
|
||||
args = parser.parse_args()
|
||||
|
||||
# 确保至少选择两个文件
|
||||
if len(args.files) < 2:
|
||||
print("请至少选择两个文件进行合并。")
|
||||
return
|
||||
|
||||
# 定义需要处理的文件
|
||||
files = [
|
||||
{'path': 'result/birth.json', 'name': 'birth'},
|
||||
{'path': 'result/astro.json', 'name': 'astro'},
|
||||
{'path': 'result/ethnic.json', 'name': 'ethnic'}
|
||||
]
|
||||
file_map = {
|
||||
'birth': 'result/birth.json',
|
||||
'astro': 'result/astro.json',
|
||||
'ethnic': 'result/ethnic.json'
|
||||
}
|
||||
|
||||
files = [{'path': file_map[file], 'name': file} for file in args.files]
|
||||
|
||||
# 处理数据
|
||||
processed_data = process_data(files)
|
||||
|
||||
# 根据输入的文件名生成 merged 文件名
|
||||
output_json_file = f'result/merged_{"_".join(args.files)}.json'
|
||||
output_csv_file = f'result/merged_{"_".join(args.files)}.csv'
|
||||
|
||||
# 确保 result 目录存在
|
||||
os.makedirs('result', exist_ok=True)
|
||||
|
||||
# 输出结果到 JSON 和 CSV 文件
|
||||
save_to_json(processed_data, 'result/merged.json')
|
||||
save_to_csv(processed_data, 'result/merged.csv')
|
||||
save_to_json(processed_data, output_json_file)
|
||||
save_to_csv(processed_data, output_csv_file)
|
||||
|
||||
print("数据处理完成,结果已保存到 result/merged.json 和 result/merged.csv")
|
||||
print(f"数据处理完成,结果已保存到 {output_json_file} 和 {output_csv_file}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user