-2

I have a .txt file with N lines. I want to read the line number 8, and the line number 16, which look like these:

2016-04-01 04:27:30.6216 (2283721) (more text)

2016-04-01 04:59:20.3635 (2283721) (more text)

How can I recover the values 04:59:20.3635 and 04:27:30.6216 and substract them? I tried with opening and reading the file using

txt = open(filename)
for line in txt:
    print line

But I don't know how to store each line in a variable, and then access the specific part of the line that I want to reach. I've tried some stuff, but I want to know which would be the most efficient way to handle this. Many thanks!!

4
  • 1
    have you googled it yet? Commented Apr 19, 2016 at 17:09
  • 2
    try similar post stackoverflow.com/questions/30627810/… Commented Apr 19, 2016 at 17:14
  • 2
    Please read The Python Tutorial. It answers many of your questions. If you still have questions, come back and ask them. Commented Apr 19, 2016 at 17:15
  • "But I don't know how to store each line in a variable, and then access the specific part of the line that I want to reach." --> have you tried actually learning a language before trying to use it? (Don't take it personally, a lot of people ask on SO instead of studying first, as if programming answers fell from the sky...wait, they do. Nevermind.) Commented Apr 26, 2016 at 20:12

3 Answers 3

0

The following is my approach. Hope it helps:

import re
from datetime import datetime

def get_date_time (t):
    """
    t: date time string;
    parse date time string, and return datetime
    """
    time_string = re.findall('\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{4}', t)[0]
    return datetime.strptime(time_string, '%Y-%m-%d %H:%M:%S.%f')

if __name__ == '__main__':    
    with open('input.txt') as f:
        src = f.read().split('\n') # split into lines
        print get_date_time(src[7]) -  get_date_time(src[5]) # calculate difference

Output:

0:31:49.741900
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that worked!! How can I convert your output to seconds? I'm trying with "strresult = str(result)" and then "print sum(int(x) * 60 ** i for i,x in enumerate(reversed(strresult.split(":"))))" but it gives me problems because of the miliseconds!
To convert to seconds, you could: delta = get_date_time(src[7]) - get_date_time(src[5]); and then print delta.seconds + delta.microseconds/1E6. The result is: 1909.7419
0

You could make an array of strings then have the file print each line into the array in a while loop. The while loop checks how much you read into the array or when to stop reading file. After, just simply use other methods to search through a specific line or array index to find what you need. There are plenty of other ways too depending on the task at hand.

1 Comment

In addition to providing a textual answer, please consider adding code. This will help future readers to get the most out of your answer.
0

python is good at manipulate string, so at your situation just

timestamps =[]
for line in fh:
    timestamps.append(line. split()[1])
print timestamps, timestamps[0]

you could even extract/ calculated the time delta by using the datetime or timedelta related build in libraries, but first, as other commented, you should go through the tutorial, believe me, it's short and worth reading if you think you would occasionally grasp some line the other day -- you need to know what python could do generally

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.