0
我的代碼如何將itertools.izip逐行寫入txt文件?
import itertools
import os
with open("base.txt") as fv, open("new2.txt", 'r') as fi, open('sortedf.txt','w') as fs:
vel = (line.strip() for line in fv)
ind = (int(line.strip()) for line in fi)
z = itertools.izip(ind, vel) # sort according to ind
# itertools.izip(vel, ind) # sort according to vel
for i, v in sorted(z):
fs.write(str(v))
我把一切都在一行
2.900000e+032.900000e+032.900000e+032.900000e+032.
當我改變 fs.write('\n'.join(str(v)))
然後我
2
.
9
0
0
0
0
0
e
+
0
32
.
9
0
0
0
0
0
e
+
0
32
.
如何獲得通過適當的一個行值輸出?
你想在每行之後換行,對不對?那麼'fs.write(str(v)+'\ n')'怎麼樣? –
@GarethMcCaughan是的,這是賴特的格式。 –