将 (x,y) 坐标列表转换为空格分隔格式的文本文件



在一个python项目中,我有一个巨大的坐标列表(x,y值(:

Coordinates = [(144282.027, 523177.144), (144281.691, 523183.33), (144280.696, 523183.275), (144280.506, 523186.767), (144272.518, 523186.348), (144272.702, 523182.862), (144269.203, 523182.673), (144269.541, 523176.471), (144274.835, 523176.759), (144275.025, 523173.261), (144281.218, 523173.598), (144281.028, 523177.09), (144282.027, 523177.144)]

要将其读取为 las 文件,我需要它以空格分隔 x y z 并返回:

144282.027 523177.144 0
144281.691 523183.33 0
....

我只在堆栈溢出上看到相反的答案:空格分隔的文本文件 ->python列表

你可以做这样的事情:

coordinates = [(144282.027, 523177.144), (144281.691, 523183.33), (144280.696, 523183.275), (144280.506, 523186.767), (144272.518, 523186.348), (144272.702, 523182.862), (144269.203, 523182.673), (144269.541, 523176.471), (144274.835, 523176.759), (144275.025, 523173.261), (144281.218, 523173.598), (144281.028, 523177.09), (144282.027, 523177.144)]
lines = ['{} {} {}n'.format(x, y, 0) for x, y in coordinates]
with open('output.txt', 'w') as outfile:
for line in lines:
outfile.write(line)

上面的代码使用以下格式将坐标写入output.txt

144282.027 523177.144 0
144281.691 523183.33 0
144280.696 523183.275 0
144280.506 523186.767 0
144272.518 523186.348 0
144272.702 523182.862 0
144269.203 523182.673 0
144269.541 523176.471 0
144274.835 523176.759 0
144275.025 523173.261 0
144281.218 523173.598 0
144281.028 523177.09 0
144282.027 523177.144 0

最新更新