-1

In A python project I have a huge list of coordinates (x,y values):

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)]

To read it as a las file I need it space seperated x y z with a return:

144282.027 523177.144 0
144281.691 523183.33 0
....

I only see answers on stackoverflow doing the opposite way: space seperated text file -> python list

Mvz
  • 507
  • 3
  • 11
  • 29
  • You cannot find a way to *join* (<- blatantly obvious hint) your values? – Jongware Oct 21 '18 at 13:12
  • 1
    If you are asking what I understand, it is quite easy. As an example to print to the screen: `for x, y in Coordinates: print(x, y, 0)` – Alperen Oct 21 '18 at 13:15
  • 1
    Possible duplicate of [Python: Write a list of tuples to a file](https://stackoverflow.com/questions/3820312/python-write-a-list-of-tuples-to-a-file) – Georgy Oct 21 '18 at 13:16

1 Answers1

2

You could do something like this:

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)

The above code write the coordinates to output.txt with the following format:

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
Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76