Python Forum
list of strings to list of float
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list of strings to list of float
#1
Hello,

i am trying to read a text file to get a list of point coordinates from a texte file :

this is the text :

-48.9701217110874,5.67558617156734,0
-43.5855912406261,6.54875327488539,0
-51.1530394693826,-3.20161271216619,0
-45.6229811483682,-1.89186205718911,0
-37.0368379657407,5.23900261990832,0
-52.8993736760187,6.54875327488539,0
-56.5375699398439,-0.727639252765042,0

i called the file polyline.txt and this how i wrote the code :


def ImportPointList():
    f = open("polyline.txt") #open the text file
    lines = f.readlines() #read the line
    lines = [item.rstrip("\n") for item in lines] #divide the line in items
    pointList = list(lines)  # create a list of items
    listLength = len(pointList) #tell me the number of items in the list
    
    print ("this is the new List",pointList,"composed of ",listLength,"items")


The problem i have now is how to transform that list of strings into a list of float numbers
How to remove the z coordinates since its 0

and if you are still interested by the problem Wall how to compute the vectors from that list of coordinates knowing that if if AB are two consecutive points
the vector slope is (Yb-Ya)/(Xb-Xa)

thanks !!!!
Reply
#2
For each line item eg. -48.9701217110874,5.67558617156734,0, you can use split() to turn it to a list. Then delete last item. Then use list comprehension to make a new list of floats.
line_item = "-48.9701217110874,5.67558617156734,0"
new_item = list(line_item.split(","))
del new_item[-1]
coordinate = [float(num) for num in new_item]
print(coordinate)
print(type(coordinate[1]))
Reply
#3
If this is not an assignment, I would recommend to use Pandas for such i/o operations. You can use pandas.read_csv function. This function automatically converts all values which look like float numbers to floats.
Another option is to use Numpy (numpy.loadtxt)

if you want to write your own function for reading this particular file,
I wouldn't read the entire file at once (f.readlines()), but read it line-by-line, e.g.

with open("polyline.txt") as f:
    result = []
    for line in f:
         row = line.split(',')[:2]
         try:
            row = map(float, row)
         except ValueError:
            print("Couldn't convert row: %s " % row)
         else:
            result.append(list(row))
Reply
#4
(Feb-17-2020, 11:40 PM)michael1789 Wrote: For each line item eg. -48.9701217110874,5.67558617156734,0, you can use split() to turn it to a list. Then delete last item. Then use list comprehension to make a new list of floats.
line_item = "-48.9701217110874,5.67558617156734,0"
new_item = list(line_item.split(","))
del new_item[-1]
coordinate = [float(num) for num in new_item]
print(coordinate)
print(type(coordinate[1]))


I am sorry but i am new to python and programming. How would you write the loop that goes threw all the lines ?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to reverse a list and store in another list in python SuperNinja3I3 6 3,295 Aug-14-2022, 06:36 PM
Last Post: DeaD_EyE
Question Python - List - Int List sophi 8 2,548 Apr-21-2022, 07:55 PM
Last Post: sophi
  Removing all strings in a list that are of x length Bruizeh 5 3,189 Aug-27-2021, 03:11 AM
Last Post: naughtyCat
  Check if a list exists in given list of lists Daniel94 2 2,238 Apr-07-2020, 04:54 PM
Last Post: deanhystad
  Convert a list of integers to strings? Cornelis 3 2,268 Nov-15-2019, 12:13 PM
Last Post: perfringo
  arrays sum list unsupported operand type(s) for +=: 'int' and 'list' DariusCG 7 4,170 Oct-20-2019, 06:24 PM
Last Post: Larz60+
  have homework to add a list to a list using append. celtickodiak 2 2,015 Oct-11-2019, 12:35 PM
Last Post: ibreeden
  Search character from 2d list to 2d list AHK2019 3 2,498 Sep-25-2019, 08:14 PM
Last Post: ichabod801
  I donr know how to search from 2d list to 2d list AHK2019 1 1,762 Sep-25-2019, 01:59 AM
Last Post: ichabod801
  sort a list alphabeticaly without changing the original list Holmen 5 4,208 Jan-27-2019, 01:49 PM
Last Post: Holmen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020