2017-05-10 19 views
-2

秒で各行にデルタ時間を追加し、以下の解決しようとして:Pythonのテキストファイルの操作は、私のpythonの初心者です

を私はそれぞれの行は次のように起動し、テキストファイルがあります。

<18:12:53.972> 
<18:12:53.975> 
<18:12:53.975> 
<18:12:53.975> 
<18:12:54.008> 

など

上記の代わりに、行の先頭に「<」という文字列がある場合にのみ、経過時間を秒単位で追加したいと考えています。など

<0.0><18:12:53.972> 
<0.003><18:12:53.975> 
<0.003><18:12:53.975> 
<0.003><18:12:53.975> 
<0.036><18:12:54.008> 

ここ

#import datetime 
from datetime import timedelta 
from sys import argv 

#get filename as argument 
run, input, output = argv 
#get number of lines for textfile 
nr_of_lines = sum(1 for line in open(input)) 
#read in file 
f = open(input) 
lines = f.readlines() 
f.close 

#declarations 
do_once = True 
time = [] 
delta_to_list = [] 
i = 0 

#read in and translate all timevalues from logfile to delta time. 
while i < nr_of_lines: 
    i += 1 
    if lines[i-1].startswith('<'): 
     get_lines = lines[i-1]  #get one line 
     get_time = (get_lines[1:13]) #get the time from that line 
     h = int(get_time[0:2]) 
     m = int(get_time[3:5]) 
     s = int(get_time[6:8]) 
     ms = int(get_time[9:13]) 
     time = timedelta(hours = h, minutes = m, seconds = s, microseconds = 0, milliseconds = ms) 
     sec_time = time.seconds + (ms/1000) 
     if do_once: 
      start_value = sec_time 
      do_once = False 
     delta = float("{0:.3f}".format(sec_time - start_value)) 
     delta_to_list.append(delta) 

#write back values to logfile. 
k=0 
s = str(delta_to_list[k]) 
with open(output, 'w') as out_file: 
    with open(input, 'r') as in_file: 
     for line in in_file: 
      if line.startswith('<'): 
       s = str(delta_to_list[k]) 
       out_file.write("<" + s + ">" + line) 
      else: 
       out_file.write(line) 
      k += 1 

:-)試みは、それが今であるので、それがうまく動作しますが、最後の2行は、新しいファイルに書かれていませんしています。それは言う:「のS = strの(delta_to_list [K])はIndexError:リストインデックスを範囲外

最初に私は私のコードは、改善のための提案を働いて、第二取得したいと思いありがとう

+4

これまでに何を試しましたか? –

+2

これはpythonで可能ですか?はい – e4c5

+2

もちろん可能です。実際、それはかなり簡単です。 datetimeモジュール – Chaker

答えて

0

。。!第1のポイント:メモリが足りないときにメモリ内のフルファイルを読むことは決してありません。(特に空きメモリがあるかどうかわからないときは)

2点目:pythonのforループと反復プロトコルlistのいずれかを反復する方法その他の反復可能なものは、

です。
for item in some_iterable: 
    do_something_with(item) 

これは、インデックスをいじり、それは間違って取得避け;)Pythonのfileオブジェクトとの素敵な物事の

一つは、彼らが実際にそのファイルの行を反復処理するために、反復可能オブジェクトであるということです、最も簡単な方法は次のとおりです。

for line in my_opened_file: 
    do_something_with(line) 

ここで、シンプルながら作業し、ほとんど神託(NB:パイソン2.7.35)がありますあなたのプログラムを書くための方法:

# -*- coding: utf-8 -*- 
import os 
import sys 
import datetime 
import re 
import tempfile 


def totime(timestr): 
    """ returns a datetime object for a "HH:MM:SS" string """ 

    # we actually need datetime objects for substraction 
    # so let's use the first available bogus date 

    # notes: 
    # `timestr.split(":")` will returns a list `["MM", "HH", "SS]` 
    # `map(int, ...)` will apply `int()` on each item 
    # of the sequence (second argument) and return 
    # the resulting list, ie 
    # `map(int, "01", "02", "03")` => `[1, 2, 3]` 

    return datetime.datetime(1900, 1, 1, *map(int, timestr.split(":"))) 

def process(instream, outstream): 
    # some may consider that regexps are not that pythonic 
    # but as far as I'm concerned it seems like a sensible 
    # use case. 
    time_re = re.compile("^<(?P<time>\d{2}:\d{2}:\d{2})\.") 
    first = None 

    # iterate over our input stream lines 
    for line in instream: 
     # should we handle this line at all ? 
     # (nb a bit redundant but faster than re.match) 
     if not line.startswith("<"): 
      continue 

     # looks like a candidate, let's try and 
     # extract the 'time' value from it 
     match = time_re.search(line) 
     if not match: 
      # starts with '<' BUT not followed by 'HH:MM:SS.' ? 
      # unexpected from the sample source but well, we 
      # can't do much about it either 
      continue 

     # retrieve the captured "time" (HH:MM:SS) part 
     current = totime(match.group("time")) 

     # store the first occurrence so we can 
     # compute the elapsed time 
     if first is None: 
      first = current 

     # `(current - first)` yields a `timedelta` object 
     # we now just have to retrieve it's `seconds` attribute 
     seconds = (current - first).seconds 

     # inject the seconds before the line 
     # and write the whole thing tou our output stream 
     newline = "{}{}".format(seconds, line) 
     outstream.write(newline) 

def usage(err=None): 
    if err: 
     print >> sys.stderr, err 
    print >> sys.stderr, "usage: python retime.py <filename>" 
    # unix standards process exit codes 
    return 2 if err else 0 

def main(*args): 
    # our entry point... 
    # gets the source filename, process it 
    # (storing the results in a temporary file), 
    # and if everything's ok replace the source file 
    # by the temporary file. 

    try: 
     sourcename = args[0] 
    except IndexError as e: 
     return usage("missing <filename> argument") 

    # `delete=False` prevents the tmp file to be 
    # deleted on closing.  
    dest = tempfile.NamedTemporaryFile(delete=False) 

    with open(sourcename) as source: 
     try: 
      process(source, dest) 
     except Exception as e: 
      dest.close() 
      os.remove(dest) 
      raise 

    # ok done 
    dest.close() 
    os.rename(dest.name, sourcename) 
    return 0 


if __name__ == "__main__": 
    # only execute main() if we are called as a script 
    # (so we can also import this file as a module) 
    sys.exit(main(*sys.argv[1:])) 

それが与えます(Linux上で動作していますが、サポートされている他のOSであれば問題ありません)。

元のコードのように動作するように記述しましたが(ソースファイルを処理済みのファイルに置き換えてください)、私のコードであれば、代わりに明示的に宛先ファイル名を指定するか、デフォルトの書き込みとしてsys.stdout stdoutを別のファイルにリダイレクトします)。 process関数は、FWIWのいずれかの解を扱うことができます。main()のいくつかの編集の唯一の問題です。

+0

助けていただきありがとうございます。あなたは私の更新されたコードを見ることができますか?今すぐ始める人として働いています:-)おかげで – Matrilx

+0

"うまくいく方法を知りませんでした"それはかなり文書化されています...それはまったく使ってみましたか? –

+0

今、あなたの助けに感謝します! – Matrilx

関連する問題