I need to copy timing window into a variable from line of .txt file using Python.

Input file:

[Fri Dec 07 18:50:16.775 2018] [    3.610065] dwc3 e2d00000.usb_core: SUCCESS allocating dump_regset memory
[Fri Dec 07 18:50:16.775 2018] [    3.610770] dwc3 e2d00000.usb_core: Soft reset timeout -29631
[Fri Dec 07 18:50:16.775 2018] [    3.614879] dwc3 e2d00000.usb_core: Dump USB registers

So from the above input file i need "3.610770" value to be copied into integer variable because that line contains "Soft reset timeout" string.

I have managed to write some portion of code but unable to proceed further.

Python script snippet:

import sys
inFile = sys.argv[1]

with open(inFile) as fp:
    line = fp.readline()

    while line:
        if "Soft reset timeout" in line:
        #print line
            if "[    " in line:
            #To Do...

        #if "Dump USB registers" in line:
        line = fp.readline()

2 Answers 2


Using string.split() you can achieve this. You need to use [ and ] to split a string into two parts. Then get the part of the string you want. To remove whitespaces you can use string.strip()

import sys
with open(input_file) as fp:
    for line in lines:
        if "Soft reset timeout" in line:

If you want integer value from the float number '3.610770' you can use int(timeout_value)


Get the part of the sub-string you want to convert into a variable (myVar). You can then cast the variable to an integer: int(myVar)

See Is there a way to substring a string? and How to convert strings into integers in Python?

Not the answer you're looking for? Browse other questions tagged or ask your own question.