I have a .txt file called geom.txt that looks like the following -3.0 2.0 # coor
ID: 3788159 • Letter: I
Question
I have a .txt file called geom.txt that looks like the following
-3.0 2.0 # coordinates of P
2.0 -1.0 # coordinates of Q
2.0 1.0 3.0 # center and radius of C
-2.0 -3.0 5.0 # center and radius of D
2.0 6.0 8.0 4.0 # coord ul and lr of rectangle G
-3.0 2.0 4.0 -3.0 # coord ul and lr of rectangle H
I am trying to find a code in PYTHON where I can get the floating numbers only and omit anything past the '#' sign
Explanation / Answer
#!/usr/bin/python
##Open the file geom.txt
fileob = open( "./geom.txt", "r")
#Read the entire file at once
str = fileob.read()
#Create a list of lines by spliting the data read from the file (assuming ' ' to be the delimiter)
lines = str.split(" ")
i = 0
#Loop over the list of lines
#Split each line using "#" as a delimiter
#The value_part[0] now contains the list of floating values in each line. Similarly split the value_part using " "(space) as a delimiter
for val in lines:
value_part = val.split("#")
float_values = value_part[0].split(" ")
print float_values
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.