Python打印字符串到文本文件我使用Python打开文本文档:text_file = open("Output.txt", "w")
text_file.write("Purchase Amount: " 'TotalAmount')
text_file.close()我想替换字符串变量的值。TotalAmount进入文本文档。有人能告诉我怎么做吗?
3 回答
一只斗牛犬
TA贡献1784条经验 获得超2个赞
price = 33.3
with open("Output.txt", "w") as text_file:
text_file.write("Purchase Amount: %s price %f" % (TotalAmount, price))
墨色风雨
TA贡献1853条经验 获得超6个赞
如果您正在使用Python 3。
your_data = {"Purchase Amount": 'TotalAmount'}
print(your_data, file=open('D:\log.txt', 'w'))
用于丙酮2
def my_func():
"""
this function return some value
:return:
"""
return 25.256
def write_file(data):
"""
this function write data to file
:param data:
:return:
"""
file_name = r'D:\log.txt'
with open(file_name, 'w') as x_file:
x_file.write('{} TotalAmount'.format(data))
def run():
data = my_func()
write_file(data)
run()添加回答
举报
0/150
提交
取消
