2013-02-13 46 views
0

我知道必須有幾種方法來做到這一點,但我很樂意聽取您的意見,哪種方法是最好的方法。重新格式化Python中的秒錶.csv

我有一個.csv,從手機的秒錶應用程序輸出,看起來像這樣:

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,, 

我需要改變秒錶時間碼爲小時:分鐘:秒:幀格式,刪除列標題,增加額外信息並考慮小時翻車(第40行以後)。

輸出因此將是這樣的:

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     

什麼是做到這一點的最有效方法是什麼?

+6

開始。任何東西。 – Blender 2013-02-13 03:10:45

+3

csv和datetime模塊是一個很好的開始。 – monkut 2013-02-13 03:13:42

+1

或者只是'str.split' ... – mgilson 2013-02-13 03:30:24

回答

0

想通了 - 因爲嘗試

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 

    for 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) successfully created:\n %s\n\n" % (new_filename) 

for root, dirs, files in os.walk(os.getcwd()): 
SW_file="" 
for filename in files: 
    if (filename.startswith("Stopwatch")): 
     if (filename.endswith(".csv")): 
     convert_stopwatch(os.path.join(root, filename))