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.

Revisions

  1. f-ewald created this gist Oct 22, 2015.
    55 changes: 55 additions & 0 deletions file_formatter.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    # 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