2

for ループで何百もの行 (x、y 座標) 出力を生成しています。プロセスの最後にそれらをtxtファイルに保存する簡単な方法は何ですか?

ありがとう

出力例: …</p>

100 23

112 18

133 67

221 99

232 100

…</p>

4

3 に答える 3

2

たとえば、通常の書き込みでは

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)
于 2013-05-24T06:17:34.637 に答える
0

Unix 環境の場合。次のコマンドを実行できます。

python *your_code.py* | tee *output_file*

出力はコンソールと *output_file* にも出力されます。

于 2013-05-24T06:17:11.413 に答える