python - Is there a more "Pythonic" way to combine CSV elements? -


basically using python cron read data web , place in csv list in 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 regex search , itterate through matches between ### , $$$, go through each match line line, taking each line , splitting commas. can see entries have 4 commas, have 5. because dumb , didn't realize web source puts commas in it's 4 digit numbers. ie

entry1,36,257.21,16.15,16.168

is suposed be

entry1,36257.21,16.15,16.168

i collected lot of data , not want rewrite, thought of cumbersome workaround. there more pythonic way this?

===

contents = ifp.read()  #pull entries market data entry in re.finditer("###(.*\n)*?\$\$\$",contents):      dataset = contents[entry.start():entry.end()]     dataset = dataset.split('\n');      timestamp = dataset[0][3:]     print timestamp      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         ## useful work data ## 

===

i'd use python's csv module read in csv file, fix broken rows encountered them, use csv.writer write csv out. (assuming original file, commas in wrong place, ugly.csv, , new, cleaned output file pretty.csv):

import csv  inputcsv = csv.reader(open("ugly.csv", "rb")) outputcsv = csv.writer(open("pretty.csv", "wb"))  row in inputcsv:   if len(row) >= 5:     row[1] = row[1] + row[2] #note csv entries strings, string concatenation, not addition     del row[2]   outputcsv.writerow(row) 

clean , simple, and, since you're using proper csv parser , writer, shouldn't have worry introducing new weird corner cases (if had used in first script, parsing web results, commas in input data have been escaped).


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -