我用Python寫了一個腳本字符串(x,y座標)的兩列之間刪除選項卡/空格加上一個逗號分隔欄和上市的最大和每列的最小值(每個x和y座標的值爲2)。例如:的Python腳本trasnforming ANS以升序排序列,小數情況下
100000.00 60000.00
200000.00 63000.00
300000.00 62000.00
400000.00 61000.00
500000.00 64000.00
變成了:
100000.00,60000.00
200000.00,63000.00
300000.00,62000.00
400000.00,61000.00
500000.00,64000.00
10000000 50000000 60000000 640000000
這是我使用的代碼:
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()
我需要改變上面的代碼也由兩個小數變換座標到小數點後一位值和兩個新列中的每一列都將根據x座標(左列)的值以升序排序。
我開始寫下面的,但它不僅是沒有排序的值,它是放置在左側和右側的x y座標。另外我不知道如何轉換小數,因爲這些值是字符串,我知道的唯一函數是使用%f並且需要浮點數。有什麼建議來改進下面的代碼?
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()
謝謝!
首先你說代碼給出了錯誤的輸出,然後你說它給了一個'IndexError'。這是什麼?在我看來,它實際上會引發'NameError',因爲沒有分配'o'。 – interjay
什麼意思是「轉換小數」,我不明白你想在這裏做什麼。你能舉一些例子嗎? –
@Tim Pietzcker:通過改變小數我的意思,而不是兩個十進制值xxxx.oo我想代碼將它們更改爲一個十進制值xxx.o – GeoLuCa