Python script for trasnforming ans sorting columns in ascending order, decimal cases
Question: I wrote a script in Python removing tabs/blank spaces between two columns of
strings (x,y coordinates) plus separating the columns by a comma and listing
the maximum and minimum values of each column (2 values for each the x and y
coordinates). E.g.:

    
    
    100000.00    60000.00
    200000.00    63000.00
    300000.00    62000.00
    400000.00    61000.00
    500000.00    64000.00
    

became:

    
    
    100000.00,60000.00
    200000.00,63000.00
    300000.00,62000.00
    400000.00,61000.00
    500000.00,64000.00
    
    10000000 50000000 60000000 640000000
    

This is the code I used:

    
    
    import string
    input = open(r'C:\coordinates.txt', 'r')
    output = open(r'C:\coordinates_new.txt', 'wb')
    s = input.readline()
    while s <> '':
        s = input.readline()
        liste = s.split()
        x = liste[0]
        y = liste[1]
        output.write(str(x) + ',' + str(y))
        output.write('\n')
        s = input.readline()
    input.close()
    output.close()
    

I need to change the above code to also transform the coordinates from two
decimal to one decimal values and each of the two new columns to be sorted in
ascending order based on the values of the x coordinate (left column).

I started by writing the following but not only is it not sorting the values,
it is placing the y coordinates on the left and the x on the right. In
addition I don't know how to transform the decimals since the values are
strings and the only function I know is using %f and that needs floats. Any
suggestions to improve the code below?

    
    
    import string
    input = open(r'C:\coordinates.txt', 'r')
    output = open(r'C:\coordinates_sorted.txt', 'wb')
    s = input.readline()
    while s <> '':
        s = input.readline()
        liste = string.split(s)
        x = liste[0]
        y = liste[1]    
        output.write(str(x) + ',' + str(y))    
        output.write('\n')
        sorted(s, key=lambda x: x[o])
        s = input.readline()
    input.close()
    output.close()
    

thanks!


Answer: Your code looks more like C than like Python; it is quite unidiomatic. I
suggest you read the [Python
tutorial](http://docs.python.org/2/tutorial/index.html) to find some
inspiration. For example, iterating using a `while` loop is usually the wrong
approach. The `string` module is deprecated for the most part, `<>` should be
`!=`, you don't need to call `str()` on an object that's already a string...

Then, there are some errors. For example, `sorted()` returns a sorted version
of the iterable you're passing - you need to assign that to something, or the
result will be discarded. But you're calling it on a string, anyway, which
won't give you the desired result. You also wrote `x[o]` where you clearly
meant `x[0]`.

You should be using something like this (assuming Python 2):

    
    
    with open(r'C:\coordinates.txt') as infile:
        values = []
        for line in infile:
            values.append(map(float, line.split()))
    values.sort()
    with open(r'C:\coordinates_sorted.txt', 'w') as outfile:
        for value in values:
            outfile.write("{:.1f},{:.1f}\n".format(*value))
    

