| import os | |
| import json | |
| import argparse | |
| def check_file(path): | |
| with open(path, "r", encoding="utf-8") as f: | |
| data = json.load(f) # 这里假设每个文件是一个 JSON 数组 | |
| total = len(data) | |
| # 转换成字符串来判断重复(保证字典可哈希) | |
| unique = len({json.dumps(item, sort_keys=True) for item in data}) | |
| duplicates = total - unique | |
| return total, duplicates | |
| def main(): | |
| parser = argparse.ArgumentParser(description="Check JSON array dataset files for row count and duplicates.") | |
| parser.add_argument("folder", help="Folder containing the JSON files") | |
| args = parser.parse_args() | |
| folder = args.folder | |
| for fname in sorted(os.listdir(folder)): | |
| fpath = os.path.join(folder, fname) | |
| if not os.path.isfile(fpath): | |
| continue | |
| if not fname.endswith(".json"): | |
| continue | |
| try: | |
| total, duplicates = check_file(fpath) | |
| print(f"{fname:25} rows={total:6} duplicates={duplicates:4}") | |
| except Exception as e: | |
| print(f"{fname:25} [ERROR: {e}]") | |
| if __name__ == "__main__": | |
| main() | |