How To Read A Specific Line And Print A Specific Position In This Line Using Python
Solution 1:
It's simple enough, I don't know why you complicated it.
f = open("file.txt", "r")
lines = [line for line in f.readlines() if line[0]=="!"]
number = float(lines[-1].split()[4])
Explanation:
f.readlines()
reads all lines and make a list of lines.
lines
is a list of lines starting with !
.
lines[-1]
selects the last lines from the list.
lines[-1].split()
will make a list of words in the last line. (split at " "
(spaces))
lines[-1].split()[4]
will select the 5th word from the list of words.
float(lines[-1].split()[4])
will convert the word to float.
Solution 2:
This is even more simple considering its the line what you want to print
#!/usr/bin/python#import rewithopen("file1.txt") as f:
lines = f.readlines()[-1]
lastline = lines.split()[-1]
print lastline
Desired ouput from the run..
bash-4.1$ ./file1.py
-125.10584323
In case you want all lines which are starting with
"!"
positionining into the last index value then you can use below ....
#!/usr/bin/pythonimport re
withopen("file1.txt") as f:
lines = f.readlines()
for line in lines:
if"!"in line:
line = line.split()[-1]
print line
Output value will be beow ..
bash-4.1$ ./file1_dup.py
-122.1058-123.1050-125.10584323
Solution 3:
I think what you're looking for here is how to cut and slice your string. I've tweaked the code you supplied above to include this string slice. Check out the first for loop :-)
def main():
oTE_list = []
oTE = []
with open("file.txt","r") as f:
text = f.readlines()
for line in text:
if line[0] == '!':
equals_index = line.index('=')
# Get everything after the equal and remove extra whitespace
after_equal = line[equals_index+1:].strip()
oTE_list.append(after_equal)
# Change Lists To Number Format
for item in oTE_list:
oTE.append(float(item))
for idx, item in enumerate(oTE_list):
if idx == len(oTE_list) - 1:
print(item)
if __name__ == '__main__':
main()
Running this I get the result:
C:\Python35\python.exeC:/Users/Frito/GitSource/help/sample.py
-125.10584323
Now you may be wondering how this is working. Your input is a bit difficult as there isn't really a good way that I know of to remove all the spaces in between the !
, T
, E
, =
and <number>
fields. In this case by looking at your data I opted to find the equal sign and just look at the data after it. The line equals_index = line.index('=')
finds the position in the string where the equal sign is at. This "position" includes spaces. This is can be seen by looking at the string's position 24. For example print(line[24])
.
Now that we can compute where the equal sign is at we can then get everything after the equal sign. In your example this is the number you wish to print to the screen with some extra spaces. If you were to print the string " abc " directly using the print
command it would print with three spaces before and after the abc
. The .strip()
function in python removes those extra spaces.
When we combine these two approaches above we can find your number and print just that number. I hope this helps!
Post a Comment for "How To Read A Specific Line And Print A Specific Position In This Line Using Python"