-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_csv_smart.py
More file actions
88 lines (71 loc) · 3.47 KB
/
Copy pathfix_csv_smart.py
File metadata and controls
88 lines (71 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import csv
import os
def fix_csv_columns(input_file, output_file):
"""
修复CSV文件的列对齐问题
如果非空列超过5列,将最后两列内容覆盖写入到存在问题和修复建议列
"""
fixed_rows = []
total_rows = 0
fixed_count = 0
with open(input_file, 'r', encoding='utf-8', newline='') as file:
reader = csv.reader(file)
for row_num, row in enumerate(reader, 1):
total_rows += 1
# 找出所有非空列
non_empty_cols = []
for i, cell in enumerate(row):
if cell.strip(): # 非空且非纯空格
non_empty_cols.append((i, cell))
# 如果非空列超过5列,进行修复
if len(non_empty_cols) > 5:
fixed_count += 1
# 创建新行,初始化为5列
new_row = [''] * 5
# 前3列保持不变(文件名、行号、问题代码)
for i in range(min(3, len(non_empty_cols))):
new_row[i] = non_empty_cols[i][1]
# 如果有超过5列的非空数据,将最后两列作为存在问题和修复建议
if len(non_empty_cols) >= 5:
# 倒数第二列作为存在问题(第4列,索引3)
new_row[3] = non_empty_cols[-2][1] if len(non_empty_cols) >= 2 else ''
# 最后一列作为修复建议(第5列,索引4)
new_row[4] = non_empty_cols[-1][1]
elif len(non_empty_cols) == 4:
# 只有4列非空数据,最后一列作为存在问题
new_row[3] = non_empty_cols[-1][1]
fixed_rows.append(new_row)
print(f"第{row_num}行已修复: {len(non_empty_cols)}列 -> 5列")
print(f" 原始非空列: {[col[1][:50] + '...' if len(col[1]) > 50 else col[1] for col in non_empty_cols]}")
print(f" 修复后: {[cell[:50] + '...' if len(cell) > 50 else cell for cell in new_row]}")
print()
else:
# 非空列不超过5列,保持原样但确保有5列
new_row = [''] * 5
for i, (_, cell) in enumerate(non_empty_cols):
if i < 5:
new_row[i] = cell
fixed_rows.append(new_row)
# 写入修复后的文件
with open(output_file, 'w', encoding='utf-8', newline='') as file:
writer = csv.writer(file)
writer.writerows(fixed_rows)
print(f"\n=== 修复完成 ===")
print(f"总行数: {total_rows}")
print(f"修复行数: {fixed_count}")
print(f"输出文件: {output_file}")
return fixed_count
if __name__ == "__main__":
input_file = "result222222.csv"
output_file = "result222222_smart_fixed.csv"
if not os.path.exists(input_file):
print(f"错误: 找不到输入文件 {input_file}")
exit(1)
print(f"开始修复CSV文件: {input_file}")
print(f"修复策略: 检测非空列数,超过5列时将最后两列作为存在问题和修复建议")
print("=" * 60)
try:
fixed_count = fix_csv_columns(input_file, output_file)
print(f"\n修复成功!共修复了 {fixed_count} 行数据")
except Exception as e:
print(f"修复过程中出现错误: {e}")