[英]Writing a “table” from Python3
如何在文件中的一行上写入多个字符串和多个变量输出以进行输出? 我知道write()只接受一个参数,但理想情况下这就是我想要实现的:
write('Temperature is', X , 'and pressure is', Y)
结果将是一张桌子。
你能帮我吗?
write('Temperature is {0} and pressure is {1})'.format(X,Y))
如果您想要更多地控制输出,可以执行以下操作:
X = 12.3
Y = 1.23
write('Temperature is {0:.1f} and pressure is {1:.2f})'.format(X,Y))
# writes 'Temperature is 12.3 and pressure is 1.2'
这里的文档和示例: http : //docs.python.org/py3k/library/string.html
f = open("somefile.txt", "w")
print('Temperature is', X , 'and pressure is', Y, file=f)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.