【发布时间】:2014-02-07 06:01:45
【问题描述】:
我在 csv 文件中有一组三维坐标,例如: [前两列分别是十进制度的经纬度,第三列是海拔]
49.000000,14.000000,47.206
55.000000,14.000000,34.727
49.000000,24.366667,31.979
55.000000,24.366667,24.744
我想按以下顺序对坐标进行排序:从西北角开始,然后是东西南角,例如:
55.000000,14.000000,34.727
55.000000,24.366667,24.744
49.000000,14.000000,47.206
49.000000,24.366667,31.979
必须保持高度,并粘在给定的 2D 坐标上。
我尝试了简单的第一列降序和第二列升序,但没有得到想要的顺序。
import csv
coordinates = [];
file_in = csv.reader(open('NewFile.csv', 'rb'), delimiter=',', quotechar='|');
file_out = '11dataout.txt'
fout=open(file_out,"w")
for row in file_in:
coordinates.append(row)
coordinates.sort(key=lambda x: x[1], reverse=True)
coordinates.sort(key=lambda x: x[2])
fout.write(str(coordinates))
fout.write('\n')
提前致谢。
【问题讨论】:
标签: python sorting csv coordinates