Created
October 22, 2015 16:12
-
-
Save f-ewald/cb00779a8d6f30549ca6 to your computer and use it in GitHub Desktop.
File formatter to add identifiers to lines in csv files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# file formatter for csv files | |
import os | |
ctr = 6 | |
cctr = 0 | |
for f in os.listdir('to import'): | |
print 'Processing file %s' % f | |
with open('to import/' + f, 'r') as h: | |
with open('export/' + f, 'w') as ex: | |
if f.endswith('electricityrates.csv'): | |
cctr += 1 | |
# skip the first line | |
h.readline() | |
for line in h.readlines(): | |
if not line.startswith(',') and not line == str(): | |
ex.write(str(ctr) + ',' + line[0:-4] + '\n') #skip the last comma | |
elif f.endswith('FuelPrice.csv'): | |
cctr += 1 | |
# skip the first line | |
h.readline() | |
for line in h.readlines(): | |
ex.write(str(ctr) + ',' + line) | |
elif f.endswith('ListOfHours.csv'): | |
cctr += 1 | |
# skip the first line | |
h.readline() | |
for line in h.readlines(): | |
ex.write(str(ctr) + ',' + line) | |
elif f.endswith('monthlydemandrates.csv'): | |
cctr += 1 | |
# skip the first line | |
h.readline() | |
for line in h.readlines(): | |
if not line.startswith(',') and not line == str(): | |
ex.write(str(ctr) + ',' + line) | |
elif f.endswith('MonthlyFee.csv'): | |
cctr += 1 | |
for line in h.readlines(): | |
ex.write(str(ctr) + ',' + line) | |
elif f.endswith('monthseason.csv'): | |
cctr += 1 | |
# skip the first line | |
h.readline() | |
for line in h.readlines(): | |
ex.write(str(ctr) + ',' + line) | |
if cctr % 6 == 0: | |
ctr += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment