Is there a more "Pythonic" way to combine CSV elements?
Question: Basically I am using a python cron to read data from the web and place it in a
CSV list in the form of:

    
    
    .....
    ###1309482902.37
    entry1,36,257.21,16.15,16.168
    entry2,4,103.97,16.36,16.499
    entry3,2,114.83,16.1,16.3
    entry4,130.69,15.6737,16.7498
    entry5,5.20,14.4,17
    $$$
    ###1309482902.37
    entry1,36,257.21,16.15,16.168
    entry2,4,103.97,16.36,16.499
    entry3,2,114.83,16.1,16.3
    entry4,130.69,15.6737,16.7498
    entry5,5.20,14.4,17
    $$$
    

.....

My code is to basically do a regex search and itterate through all the matches
between ### and $$$, then go through each match line by line, taking each line
and splitting by commas. As you can see some entries have 4 commas, some have
5. That is because I was dumb and didn't realize the web source puts commas in
it's 4 digit numbers. IE

entry1,36,257.21,16.15,16.168

is suposed to really be

entry1,36257.21,16.15,16.168

I already collected a lot of data and do not want to rewrite, so I thought of
a cumbersome workaround. Is there a more pythonic way to do this?

===

    
    
    contents = ifp.read()
    
    #Pull all entries from the market data
    for entry in re.finditer("###(.*\n)*?\$\$\$",contents):
    
        dataSet = contents[entry.start():entry.end()]
        dataSet = dataSet.split('\n');
    
        timeStamp = dataSet[0][3:]
        print timeStamp
    
        for i in xrange(1,8):
            splits = dataSet[i].split(',')
            if(len(splits) == 5):
                remove = splits[1]
                splits[2] = splits[1] + splits[2]
                splits.remove(splits[1])
            print splits
            ## DO SOME USEFUL WORK WITH THE DATA ##
    

===


Answer: I'd use Python's [`csv` module](http://docs.python.org/library/csv.html) to
read in the CSV file, fix the broken rows as I encountered them, then use
`csv.writer` to write the CSV back out. Like so (assuming your original file,
with commas in the wrong place, is `ugly.csv`, and the new, cleaned up output
file will be `pretty.csv`):

    
    
    import csv
    
    inputCsv = csv.reader(open("ugly.csv", "rb"))
    outputCsv = csv.writer(open("pretty.csv", "wb"))
    
    for row in inputCsv:
      if len(row) >= 5:
        row[1] = row[1] + row[2] #note that csv entries are strings, so this is string concatenation, not addition
        del row[2]
      outputCsv.writerow(row)
    

Clean and simple, and, since you're using the proper CSV parser and writer,
you shouldn't have to worry about introducing any new weird corner cases (if
you had used this in your first script, parsing web results, your commas in
your input data would have been escaped).

