I am generating hundreds of lines (x,y coordinates) output in a for loop. What would be the easy way to save them into a txt file at the end of the process?
Thanks
Example of output: …
100 23
112 18
133 67
221 99
232 100
…
Assuming coordinates
is a sequence of x
, y
pairs
import csv
with open('out.txt', 'wb') as f:
csv.writer(f, delimiter=' ').writerows(coordinates)
For example with regular write
with open('filename', 'w') as fh:
for x, y in coodrinates:
fh.write('{} {}\n'.format(x, y))
or with JSON
with open('filename', 'w') as fh:
json.dump(coordinates, fh, indent=1)
or with CSV
with open('filename', 'w') as fh:
spamwriter = csv.writer(fh)
for t in coordinates:
spamwriter.writerow(t)
If you are in a Unix environment. You could run this command:
python *your_code.py* | tee *output_file*
The output will print to console and the *output_file* as well.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.