-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodify_csv.py
More file actions
40 lines (36 loc) · 1.53 KB
/
modify_csv.py
File metadata and controls
40 lines (36 loc) · 1.53 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
import os
def main():
csv_name = 'progress.csv'
modifies = {
'EpRetTest': 'EpRetDirectTransfer'
}
script_path = os.path.dirname(os.path.abspath(__file__))
for root, dir_name, file_name in os.walk(script_path, followlinks=True):
for f_name in file_name:
if f_name == csv_name:
print(f'{os.path.join(root, f_name)}')
full_path = os.path.join(root, f_name)
with open(full_path, 'r') as f:
header = f.readline()
splited = header.split(',')
idx_dict = dict()
for ind, item in enumerate(splited):
for k, v in modifies.items():
if k == item:
idx_dict[k] = ind
if v == item:
idx_dict[v] = ind
if len(idx_dict) == 1:
continue
else:
with open(full_path + '123', 'w') as f_w:
f_w.write(header)
for line in f:
items = line.split(',')
for k, v in modifies.items():
items[idx_dict[k]] = items[idx_dict[v]]
line = ','.join(items)
f_w.write(line)
os.system(f'mv {full_path}123 {full_path}')
if __name__ == '__main__':
main()