0

I wrote the following function:

def read_coordinates(filename):
    invoerfile = open(filename)
    lines = invoerfile.readlines()
    for line in lines:
        seperate_coordinate_rows = line.split("=")
        for seperate_coordinate_row in seperate_coordinate_rows:
            row = seperate_coordinate_row.split()
            print row

which gives me these lists:

['5,4', '4,5', '8,7']
['6,3', '3,2', '9,6', '4,3']
['7,6']
['9,8']
['5,5', '7,8', '6,5', '6,4']

What do I need to add to this function to get lists with floats as output?

4
  • Can you post an example of the input? Commented Nov 28, 2013 at 13:20
  • If you post a sample of your input, someone here can possible come up with a better solution than this. Commented Nov 28, 2013 at 13:25
  • Instead of ['7,6'], it should be [7.6]. Is this what you want? Or should it be [7,6]? Commented Nov 28, 2013 at 13:30
  • Iw want 7.6, the answer I accepted was exactly what is was looking for Commented Nov 28, 2013 at 13:34

5 Answers 5

4

You can use this:

row = map(lambda x: float(x.replace(',','.')), row)

The above will return a generator in Python 3.x, which may or may not suit your needs. If you need an actual list, you have two options:

# Convert the generator to a list (bad option)
row = list(row)

# Or use a list comprehension
row = [float(x.replace(',','.')) for x in row]
Sign up to request clarification or add additional context in comments.

3 Comments

This will return a generator in Python 3.x
Why not use a list comprehension here? [float(x.replace(',', '.')) for x in row]. It'll be faster than the map (no stack push), and work the same in both Python 2 and 3.
Fixed it, although I don't think the OP is using Python 3.x, nor will he care about the really tiny performance improvement of using listcomp vs maps.
0

Put this inside your inner for loop.

row = [float(x.replace(',', '.') for x in row]

Comments

0

To make sure you get a list it's best to use a list comprehension, since map doesn't return a list in Python3

row = [float(x.replace(",", ".")) for x in seperate_coordinate_row.split()]

Comments

0

replace the last two lines with

row = [float(item.replace(',', '.')) for item in seperate_coordinate_row.split()]
print row

Comments

0

Map your values to float():

row = map(float, seperate_coordinate_row.replace(',', '.').split())

map() calls the first argument for each value in the second value, effectively returning a list of float values.

The .replace() here is needed to turn your format to one that float() recognizes.

Demo:

>>> line = '5,4 4,5 8,7'
>>> map(float, line.replace(',', '.').split())
[5.4, 4.5, 8.7]

If you are using Python 3, or just want to use a list comprehension anyway, use:

[float(item.replace(',', '.') for item in seperate_coordinate_row.split()]

Here the .replace() is applied to each split item instead.

2 Comments

You'd first need to replace "," with ".".
Shouldnt we use locales to process this data?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.