Skip to content

Instantly share code, notes, and snippets.

@f-ewald
Created October 22, 2015 16:12
Show Gist options
  • Save f-ewald/cb00779a8d6f30549ca6 to your computer and use it in GitHub Desktop.
Save f-ewald/cb00779a8d6f30549ca6 to your computer and use it in GitHub Desktop.
File formatter to add identifiers to lines in csv files
# 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