for ループで何百もの行 (x、y 座標) 出力を生成しています。プロセスの最後にそれらをtxtファイルに保存する簡単な方法は何ですか?
ありがとう
出力例: …</p>
100 23
112 18
133 67
221 99
232 100
…</p>
たとえば、通常の書き込みでは
with open('filename', 'w') as fh:
for x, y in coodrinates:
fh.write('{} {}\n'.format(x, y))
またはJSONを使用
with open('filename', 'w') as fh:
json.dump(coordinates, fh, indent=1)
またはCSVで
with open('filename', 'w') as fh:
spamwriter = csv.writer(fh)
for t in coordinates:
spamwriter.writerow(t)
Unix 環境の場合。次のコマンドを実行できます。
python *your_code.py* | tee *output_file*
出力はコンソールと *output_file* にも出力されます。