Thursday, 15 May 2014

Reformatting a stopwatch .csv in Python -



Reformatting a stopwatch .csv in Python -

i know there must few ways of doing this, i'd love sentiment on best way so.

i have .csv, outputted phone's stopwatch application looks this:

no.,split time,, 1,+03:16.110,, 2,+12:23.120,, 3,+15:36.187,, 4,+16:56.487,, 5,+19:30.488,, 6,+20:01.621,, [...] 37,+53:01.921,, 38,+53:39.738,, 39,+53:40.241,, 40,+01:06.849,, 41,+01:16.442,,

i need alter stopwatch timecode hours:minutes:seconds:frames format, remove column headings, add together info , take consideration hr roll-over (row 40 onwards).

the output hence this:

cut_v01 00:03:16:00 v4 black cut_v01 00:12:23:00 v4 black cut_v01 00:15:36:00 v4 black cut_v01 00:16:56:00 v4 black cut_v01 00:19:30:00 v4 black cut_v01 00:20:01:00 v4 black [...] cut_v01 00:53:01:00 v4 black cut_v01 00:53:39:00 v4 black cut_v01 00:53:40:00 v4 black cut_v01 01:01:06:00 v4 black cut_v01 01:01:16:00 v4 black

what efficient way this?

figured out -

import string, sys, os def convert_stopwatch(in_filename): new_filename=in_filename.replace("stopwatch", "timecode") sw_lines=open(in_filename,'ur').readlines() timecode=open(new_filename, 'w') previous_minutes = 0 hours = 0 sw_line in sw_lines: plus_pos = sw_line.find("+") if plus_pos > -1: dot_pos = sw_line.find(".") sw_time = sw_line[plus_pos + 1:dot_pos] minutes = int(sw_time.split(":")[0]) if minutes < previous_minutes: hours = hours + 1 previous_minutes = minutes timecode.write("cut_v01 %02d:%s:00 v4 black\n" % (hours,sw_time) ) print "timecode file(s) created:\n %s\n\n" % (new_filename) root, dirs, files in os.walk(os.getcwd()): sw_file="" filename in files: if (filename.startswith("stopwatch")): if (filename.endswith(".csv")): convert_stopwatch(os.path.join(root, filename))

python csv formatting multiple-columns calculated-columns

No comments:

Post a Comment