在 Python 中,如何在保存到文件时格式化字符串
In Python, how to format string while saving in a file
是否可以在保存到文件时格式化字符串,类似于打印到控制台时?
f = open('test.txt', 'w')
f.write('{0:10} {1:10}').format('one', 'two')
f.close()
是的。我认为您的右括号之一不是您想要的位置:
with open('test.txt', 'w') as f:
f.write('{0:10} {1:10}'.format('one', 'two'))
是否可以在保存到文件时格式化字符串,类似于打印到控制台时?
f = open('test.txt', 'w')
f.write('{0:10} {1:10}').format('one', 'two')
f.close()
是的。我认为您的右括号之一不是您想要的位置:
with open('test.txt', 'w') as f:
f.write('{0:10} {1:10}'.format('one', 'two'))