diff options
author | mlot <petri-rush-curvy@duck.com> | 2025-06-06 13:40:57 -0400 |
---|---|---|
committer | mlot <petri-rush-curvy@duck.com> | 2025-06-06 13:40:57 -0400 |
commit | 75a42ec54dbf721caa659ddf02c1f46fc2cb4bef (patch) | |
tree | 84be794a2481e356a7784557a6f9fb6fbf29cfdd /Chapter14ProjRemoveHeadersCsv.py |
Diffstat (limited to 'Chapter14ProjRemoveHeadersCsv.py')
-rw-r--r-- | Chapter14ProjRemoveHeadersCsv.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/Chapter14ProjRemoveHeadersCsv.py b/Chapter14ProjRemoveHeadersCsv.py new file mode 100644 index 0000000..cf3d205 --- /dev/null +++ b/Chapter14ProjRemoveHeadersCsv.py @@ -0,0 +1,33 @@ +#! python3 + +# Chapter 14 Project Removing the Header from CSV Files +# Removes the header from all CSV files in the current working +# directory. + +import os +import csv + +os.makedirs('headerRemoved', exist_ok=True) + +for csvFilename in os.listdir('.'): + if not csvFilename.endswith('.csv'): + continue + + print('Removing header from ' + csvFilename + '...') + + csvRows = [] + csvFileObj = open(csvFilename) + readerObj = csv.reader(csvFileObj) + for row in readerObj: + if readerObj.line_num == 1: + continue + csvRows.append(row) + csvFileObj.close() + + csvFileObj = open(os.path.join('headerRemoved', csvFilename), 'w', newline='') + csvWriter = csv.writer(csvFileObj) + for row in csvRows: + csvWriter.writerow(row) + csvFileObj.close() + + |