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











up vote
0
down vote

favorite












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















put on hold as off-topic by Kusalananda, Bananguin, msp9011, Rui F Ribeiro, JigglyNaga yesterday



  • 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
    yesterday















up vote
0
down vote

favorite












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















put on hold as off-topic by Kusalananda, Bananguin, msp9011, Rui F Ribeiro, JigglyNaga yesterday



  • 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
    yesterday













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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 yesterday









Rui F Ribeiro

38.6k1479128




38.6k1479128










asked yesterday









Shivaprasad A Prabhu

316




316




put on hold as off-topic by Kusalananda, Bananguin, msp9011, Rui F Ribeiro, JigglyNaga yesterday



  • 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.




put on hold as off-topic by Kusalananda, Bananguin, msp9011, Rui F Ribeiro, JigglyNaga yesterday



  • 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
    yesterday


















  • 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
    yesterday
















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
yesterday




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
yesterday










2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










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
    yesterday


















up vote
0
down vote













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








    up vote
    1
    down vote



    accepted










    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
      yesterday















    up vote
    1
    down vote



    accepted










    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
      yesterday













    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    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 yesterday

























    answered yesterday









    Dipankar Nalui

    44918




    44918












    • Thanks a lot. It works as expected.
      – Shivaprasad A Prabhu
      yesterday


















    • Thanks a lot. It works as expected.
      – Shivaprasad A Prabhu
      yesterday
















    Thanks a lot. It works as expected.
    – Shivaprasad A Prabhu
    yesterday




    Thanks a lot. It works as expected.
    – Shivaprasad A Prabhu
    yesterday












    up vote
    0
    down vote













    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

























      up vote
      0
      down vote













      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























        up vote
        0
        down vote










        up vote
        0
        down vote









        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 yesterday









        Claus Andersen

        1,614414




        1,614414















            Popular posts from this blog

            Accessing regular linux commands in Huawei's Dopra Linux

            Can't connect RFCOMM socket: Host is down

            Kernel panic - not syncing: Fatal Exception in Interrupt