Copying portion of line as integer in Python scripting language [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0














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()









share|improve this question















closed as off-topic by Kusalananda, Bananguin, msp9011, Rui F Ribeiro, JigglyNaga Dec 13 at 10:49



  • This question does not appear to be about Unix or Linux within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.












  • Reading 3.610770 as an integer would result in reading 3. Is that really what you are looking for? Have you tried looking for an answer to your question yet?
    – Bananguin
    Dec 13 at 8:19















0














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()









share|improve this question















closed as off-topic by Kusalananda, Bananguin, msp9011, Rui F Ribeiro, JigglyNaga Dec 13 at 10:49



  • This question does not appear to be about Unix or Linux within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.












  • Reading 3.610770 as an integer would result in reading 3. Is that really what you are looking for? Have you tried looking for an answer to your question yet?
    – Bananguin
    Dec 13 at 8:19













0












0








0







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()









share|improve this question















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()






python python3






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 13 at 8:26









Rui F Ribeiro

38.8k1479128




38.8k1479128










asked Dec 13 at 7:53









Shivaprasad A Prabhu

316




316




closed as off-topic by Kusalananda, Bananguin, msp9011, Rui F Ribeiro, JigglyNaga Dec 13 at 10:49



  • This question does not appear to be about Unix or Linux within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by Kusalananda, Bananguin, msp9011, Rui F Ribeiro, JigglyNaga Dec 13 at 10:49



  • This question does not appear to be about Unix or Linux within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.











  • Reading 3.610770 as an integer would result in reading 3. Is that really what you are looking for? Have you tried looking for an answer to your question yet?
    – Bananguin
    Dec 13 at 8:19
















  • Reading 3.610770 as an integer would result in reading 3. Is that really what you are looking for? Have you tried looking for an answer to your question yet?
    – Bananguin
    Dec 13 at 8:19















Reading 3.610770 as an integer would result in reading 3. Is that really what you are looking for? Have you tried looking for an answer to your question yet?
– Bananguin
Dec 13 at 8:19




Reading 3.610770 as an integer would result in reading 3. Is that really what you are looking for? Have you tried looking for an answer to your question yet?
– Bananguin
Dec 13 at 8:19










2 Answers
2






active

oldest

votes


















1














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
input_file=sys.argv[1]
with open(input_file) as fp:
lines=fp.readlines()
for line in lines:
if "Soft reset timeout" in line:
#print(line)
second_field=line.split("[")[2]
timeout_str=second_field.split("]")[0]
timeout_value=timeout_str.strip()
print(timeout_value)


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






share|improve this answer






















  • Thanks a lot. It works as expected.
    – Shivaprasad A Prabhu
    Dec 13 at 8:52


















0














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?






share|improve this answer



























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    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
    input_file=sys.argv[1]
    with open(input_file) as fp:
    lines=fp.readlines()
    for line in lines:
    if "Soft reset timeout" in line:
    #print(line)
    second_field=line.split("[")[2]
    timeout_str=second_field.split("]")[0]
    timeout_value=timeout_str.strip()
    print(timeout_value)


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






    share|improve this answer






















    • Thanks a lot. It works as expected.
      – Shivaprasad A Prabhu
      Dec 13 at 8:52















    1














    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
    input_file=sys.argv[1]
    with open(input_file) as fp:
    lines=fp.readlines()
    for line in lines:
    if "Soft reset timeout" in line:
    #print(line)
    second_field=line.split("[")[2]
    timeout_str=second_field.split("]")[0]
    timeout_value=timeout_str.strip()
    print(timeout_value)


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






    share|improve this answer






















    • Thanks a lot. It works as expected.
      – Shivaprasad A Prabhu
      Dec 13 at 8:52













    1












    1








    1






    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
    input_file=sys.argv[1]
    with open(input_file) as fp:
    lines=fp.readlines()
    for line in lines:
    if "Soft reset timeout" in line:
    #print(line)
    second_field=line.split("[")[2]
    timeout_str=second_field.split("]")[0]
    timeout_value=timeout_str.strip()
    print(timeout_value)


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






    share|improve this answer














    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
    input_file=sys.argv[1]
    with open(input_file) as fp:
    lines=fp.readlines()
    for line in lines:
    if "Soft reset timeout" in line:
    #print(line)
    second_field=line.split("[")[2]
    timeout_str=second_field.split("]")[0]
    timeout_value=timeout_str.strip()
    print(timeout_value)


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







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 13 at 8:57

























    answered Dec 13 at 8:35









    Dipankar Nalui

    45718




    45718











    • Thanks a lot. It works as expected.
      – Shivaprasad A Prabhu
      Dec 13 at 8:52
















    • Thanks a lot. It works as expected.
      – Shivaprasad A Prabhu
      Dec 13 at 8:52















    Thanks a lot. It works as expected.
    – Shivaprasad A Prabhu
    Dec 13 at 8:52




    Thanks a lot. It works as expected.
    – Shivaprasad A Prabhu
    Dec 13 at 8:52













    0














    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?






    share|improve this answer

























      0














      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?






      share|improve this answer























        0












        0








        0






        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?






        share|improve this answer












        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?







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 13 at 8:22









        Claus Andersen

        1,614414




        1,614414












            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?