Spaces:
Paused
Paused
Add code/cube3d/training/find_duplicate_dat_keys.py
Browse files
code/cube3d/training/find_duplicate_dat_keys.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
from collections import defaultdict
|
| 4 |
+
|
| 5 |
+
def find_duplicate_dat_keys(json_file_path, output_dir="./duplicate_reports"):
|
| 6 |
+
"""
|
| 7 |
+
检测JSON中重复的.dat键名,记录重复次数和位置,并生成报告
|
| 8 |
+
:param json_file_path: JSON文件路径
|
| 9 |
+
:param output_dir: 报告输出目录
|
| 10 |
+
"""
|
| 11 |
+
try:
|
| 12 |
+
# 创建输出目录
|
| 13 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 14 |
+
report_path = os.path.join(output_dir, "duplicate_keys_report.txt")
|
| 15 |
+
|
| 16 |
+
# 第一步:读取原始JSON内容,记录所有.dat键名及其出现位置(行号)
|
| 17 |
+
key_positions = defaultdict(list) # {键名: [行号1, 行号2, ...]}
|
| 18 |
+
all_keys = [] # 存储所有键名(保留顺序)
|
| 19 |
+
line_num = 0
|
| 20 |
+
|
| 21 |
+
with open(json_file_path, 'r', encoding='utf-8') as f:
|
| 22 |
+
for line in f:
|
| 23 |
+
line_num += 1
|
| 24 |
+
stripped_line = line.strip()
|
| 25 |
+
|
| 26 |
+
# 匹配格式:"xxx.dat": ...(考虑可能的空格和引号)
|
| 27 |
+
if '"' in stripped_line and '.dat"' in stripped_line and ':' in stripped_line:
|
| 28 |
+
# 提取键名(处理可能的空格,如 " 3070b.dat " : 8 )
|
| 29 |
+
parts = stripped_line.split('"')
|
| 30 |
+
if len(parts) >= 3:
|
| 31 |
+
key = parts[1].strip()
|
| 32 |
+
if key.endswith('.dat'):
|
| 33 |
+
key_positions[key].append(line_num)
|
| 34 |
+
all_keys.append(key)
|
| 35 |
+
|
| 36 |
+
# 第二步:分析重复键
|
| 37 |
+
total_original = len(all_keys)
|
| 38 |
+
unique_keys = len(key_positions)
|
| 39 |
+
duplicate_keys = {k: v for k, v in key_positions.items() if len(v) > 1}
|
| 40 |
+
total_duplicates = len(duplicate_keys)
|
| 41 |
+
total_overwritten = total_original - unique_keys # 被覆盖的键数量
|
| 42 |
+
|
| 43 |
+
# 第三步:生成报告
|
| 44 |
+
with open(report_path, 'w', encoding='utf-8') as report:
|
| 45 |
+
# 报告头部
|
| 46 |
+
report.write("=" * 80 + "\n")
|
| 47 |
+
report.write(f"JSON重复.dat键名检测报告\n")
|
| 48 |
+
report.write(f"检测文件: {json_file_path}\n")
|
| 49 |
+
report.write(f"检测时间: {os.popen('date').read().strip()}\n")
|
| 50 |
+
report.write("=" * 80 + "\n\n")
|
| 51 |
+
|
| 52 |
+
# 统计信息
|
| 53 |
+
report.write(f"【基本统计】\n")
|
| 54 |
+
report.write(f"1. 原始.dat键名总数量: {total_original} 个\n")
|
| 55 |
+
report.write(f"2. 唯一.dat键名数量: {unique_keys} 个\n")
|
| 56 |
+
report.write(f"3. 存在重复的.dat键名数量: {total_duplicates} 个\n")
|
| 57 |
+
report.write(f"4. 被覆盖的键数量 (重复导致): {total_overwritten} 个\n\n")
|
| 58 |
+
|
| 59 |
+
# 重复键详情
|
| 60 |
+
if duplicate_keys:
|
| 61 |
+
report.write("=" * 80 + "\n")
|
| 62 |
+
report.write(f"【重复键详情】(共{total_duplicates}个)\n")
|
| 63 |
+
report.write("=" * 80 + "\n")
|
| 64 |
+
|
| 65 |
+
# 按重复次数排序(从高到低)
|
| 66 |
+
sorted_duplicates = sorted(duplicate_keys.items(),
|
| 67 |
+
key=lambda x: len(x[1]),
|
| 68 |
+
reverse=True)
|
| 69 |
+
|
| 70 |
+
for i, (key, positions) in enumerate(sorted_duplicates, 1):
|
| 71 |
+
repeat_count = len(positions)
|
| 72 |
+
report.write(f"\n{i}. 键名: {key}\n")
|
| 73 |
+
report.write(f" - 重复次数: {repeat_count} 次 (实际保留1次,被覆盖{repeat_count-1}次)\n")
|
| 74 |
+
report.write(f" - 出现行号: {', '.join(map(str, positions))}\n")
|
| 75 |
+
report.write(f" - 建议: 检查上述行号的键值对,保留需要的条目\n")
|
| 76 |
+
else:
|
| 77 |
+
report.write("【重复键详情】\n未发现任何重复的.dat键名\n")
|
| 78 |
+
|
| 79 |
+
report.write("\n" + "=" * 80 + "\n")
|
| 80 |
+
report.write("报告结束。重复键可能导致数据解析不完整,请根据实际需求处理。\n")
|
| 81 |
+
|
| 82 |
+
# 控制台输出摘要
|
| 83 |
+
print("=" * 80)
|
| 84 |
+
print(f"JSON重复键检测完成!")
|
| 85 |
+
print(f"检测文件: {json_file_path}")
|
| 86 |
+
print("=" * 80)
|
| 87 |
+
print(f"基本统计:")
|
| 88 |
+
print(f" - 原始键总数: {total_original}")
|
| 89 |
+
print(f" - 唯一键数量: {unique_keys}")
|
| 90 |
+
print(f" - 重复键数量: {total_duplicates}")
|
| 91 |
+
print(f" - 被覆盖键数量: {total_overwritten}")
|
| 92 |
+
print("=" * 80)
|
| 93 |
+
print(f"详细报告已保存至: {report_path}")
|
| 94 |
+
if duplicate_keys:
|
| 95 |
+
print(f"前5个重复键示例:")
|
| 96 |
+
for i, (key, positions) in enumerate(sorted(duplicate_keys.items())[:5], 1):
|
| 97 |
+
print(f" {i}. {key} (重复{len(positions)}次,行号: {positions[:3]}...)")
|
| 98 |
+
|
| 99 |
+
return duplicate_keys
|
| 100 |
+
|
| 101 |
+
except FileNotFoundError:
|
| 102 |
+
print(f"❌ 错误: 未找到JSON文件 -> {json_file_path}")
|
| 103 |
+
return None
|
| 104 |
+
except Exception as e:
|
| 105 |
+
print(f"❌ 检测失败: {str(e)}")
|
| 106 |
+
return None
|
| 107 |
+
|
| 108 |
+
if __name__ == "__main__":
|
| 109 |
+
# 替换为你的JSON文件路径
|
| 110 |
+
JSON_FILE_PATH = "/public/home/wangshuo/gap/assembly/data/car_1k/subset_self/label_mapping_freq.json"
|
| 111 |
+
find_duplicate_dat_keys(JSON_FILE_PATH)
|
| 112 |
+
|