I have a txt file called geomtxt that looks like the followi

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

Solution

#!/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

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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site