-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_filename_to_csv_header.py
More file actions
35 lines (29 loc) · 1.08 KB
/
add_filename_to_csv_header.py
File metadata and controls
35 lines (29 loc) · 1.08 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
#!/usr/bin/python
# In a directory holding all input .csv files, run the command:
# "for file in *.csv; do python3 add_filename_to_csv_header.py $file; done"
#It will output .csv files with the name of _filename.csv in the same directory.
import sys
import re
usage = "usage: python add_filename_to_header.py incsvfile"
if len(sys.argv) < 2:
sys.exit(usage)
infile = sys.argv[1]
outfile = infile.replace(".csv", "_filename.csv")
removenamestatus = {}
count, removecount, remaincount, flag = 0, 0, 0 ,0
print("=== processing "+infile+" ===")
filename = infile.split('/')[-1]
with open(infile, "r") as ifp:
with open(outfile, "w") as ofp:
for line in ifp:
line = line.strip()
if line:
count += 1
if count == 1: # headers
headers = line.split(',')
newheaders = []
for header in headers:
newheaders.append(header+"_"+filename)
ofp.write(','.join(newheaders)+"\n")
else:
ofp.write(f"{line}\n")