Copying portion of line as integer in Python scripting language [closed]
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
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.
add a comment |
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
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.
Reading3.610770
as an integer would result in reading3
. 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
add a comment |
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
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
python python3
edited Dec 13 at 8:26
Rui F Ribeiro
38.8k1479129
38.8k1479129
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.
Reading3.610770
as an integer would result in reading3
. 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
add a comment |
Reading3.610770
as an integer would result in reading3
. 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
add a comment |
2 Answers
2
active
oldest
votes
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)
Thanks a lot. It works as expected.
– Shivaprasad A Prabhu
Dec 13 at 8:52
add a comment |
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?
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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)
Thanks a lot. It works as expected.
– Shivaprasad A Prabhu
Dec 13 at 8:52
add a comment |
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)
Thanks a lot. It works as expected.
– Shivaprasad A Prabhu
Dec 13 at 8:52
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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?
add a comment |
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?
add a comment |
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?
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?
answered Dec 13 at 8:22
Claus Andersen
1,614414
1,614414
add a comment |
add a comment |
Reading
3.610770
as an integer would result in reading3
. 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