0

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!

4

2 回答 2

0

你的代码看起来更像 C 而不是 Python;这是很不习惯的。我建议你阅读Python 教程以找到一些灵感。例如,使用while循环进行迭代通常是错误的方法。该string模块大部分已弃用,<>应该是!=,您不需要调用str()已经是字符串的对象...

然后,有一些错误。例如,sorted()返回您传递的迭代的排序版本 - 您需要将其分配给某些东西,否则结果将被丢弃。但是无论如何,你是在一个字符串上调用它,这不会给你想要的结果。你还写x[o]了你明确的意思x[0]

你应该使用这样的东西(假设 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))
于 2013-09-22T12:27:58.890 回答
0

首先,尝试根据PEP8格式化你的代码——这样会更容易阅读。(我已经在你的帖子中完成了清理工作)。

其次,Tim 是对的,您应该尝试学习如何将代码编写为(惯用的)Python,而不仅仅是直接从其 C 等价物翻译而来。

作为起点,我将在此处发布您的第二个片段,重构为惯用的 Python:

# there is no need to import the `string` module; `.strip()` is a built-in
# method of strings (i.e. objects of type `str`).

# read in the data as a list of pairs of raw (i.e. unparsed) coordinates in
# string form:
with open(r'C:\coordinates.txt') as in_file:
    coords_raw = [line.strip().split() for line in in_file.readlines()]

# convert the raw list into a list of pairs (2-tuples) containing the parsed
# (i.e. float not string) data:
coord_pairs = [(float(x_raw), float(y_raw)) for x_raw, y_raw in coords_raw]

coord_pairs.sort()  # you want to sort the entire data set, not just values on
                    # individual lines as in your original snippet

# build a list of all x and y values we have (this could be done in one line
# using some `zip()` hackery, but I'd like to keep it readable (for you at
# least)):
all_xs = [x for x, y in coord_pairs]
all_ys = [y for x, y in coord_pairs]
# compute min and max:
x_min, x_max = min(all_xs), max(all_xs)
y_min, y_max = min(all_ys), max(all_ys)

# NOTE: the above section performs well for small data sets; for large ones, you
# should combine the 4 lines in a single for loop so as to NOT have to read
# everything to memory and iterate over the data 6 times.

# write everything out
with open(r'C:\coordinates_sorted.txt', 'wb') as out_file:
    # here, we're doing 3 things in one line:
    #   * iterate over all coordinate pairs and convert the pairs to the string
    #     form
    #   * join the string forms with a newline character
    #   * write the result of the join+iterate expression to the file
    out_file.write('\n'.join('%f,%f' % (x, y) for x, y in coord_pairs))

    out_file.write('\n\n')
    out_file.write('%f %f %f %f' % (x_min, x_max, y_min, y_max))

with open(...) as <var_name>为您保证关闭文件句柄,如try-finally; 此外,它比单独的行open(...)更短。.close()此外,with可用于其他目的,但通常用于处理文件。除了您可能在这里学到的所有其他内容之外,我建议您查看如何在 Python 中使用try-finally以及/context 管理器。with

于 2013-09-22T13:02:26.013 回答