我想在一个新的文本文件中写入最终结果(输出),但我不知道如何,因为 write
只接受一个参数。有什么想法吗?
a = [1, 2, 3]
b = ['one', 'two', 'three']
for x, i in zip(a, b):
print(x, i)
输出:
0 one
1 two
2 three
我的问题:
a = [1, 2, 3]
b = ['one', 'two', 'three']
for x, i in zip(a, b):
f = open("newfile.txt", "w+")
f.write("?") # HERE
f.close
f = open("newfile.txt", "r")
答案 0 :(得分:2)
f.write
要求您明确构造要写入的字符串。 print
更简单。
with open('newfile.txt', 'w') as f:
for x, i in zip(a, b):
print(x, i, file=f)
您可以使用 f.write(f'{x} {i}\n')
代替 print
。