-
Notifications
You must be signed in to change notification settings - Fork 0
/
removecsheader.py
33 lines (25 loc) · 932 Bytes
/
removecsheader.py
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
#! python3
# removeCsvHeader.py - Removes the header from all CSV files in the current
# working directory.
import csv, os
os.makedirs('headerRemoved', exist_ok=True)
# Loop through every file in the current working directory.
for csvFilename in os.listdir('.'):
if not csvFilename.endswith('.csv'):
continue # skip non-csv files
print('Removing header from ' + csvFilename + '...')
# TODO: Read the CSV file in (skipping first row).
csvRows = []
csvFileObj = open(csvFilename)
readerOBj = csv.reader(csvFileObj)
for row in readerOBj:
if readerOBj.line_num == 1:
continue # skip first row
csvRows.append(row)
csvFileObj.close()
# TODO: Write out the CSV file.
csvFileObj = open(os.path.join('headerRemoved', csvFilename), 'w', newline='')
csvWriter = csv.writer(csvFileObj)
for row in csvRows:
csvWriter.writerow(row)
csvFileObj.close()