Python打印字符串到文本文件我使用Python打开文本文档:text_file = open("Output.txt", "w")
text_file.write("Purchase Amount: " 'TotalAmount')
text_file.close()我想替换字符串变量的值。TotalAmount进入文本文档。有人能告诉我怎么做吗?
3 回答
当年话下
TA贡献1890条经验 获得超9个赞
price = 33.3 with open("Output.txt", "w") as text_file: text_file.write("Purchase Amount: %s price %f" % (TotalAmount, price))
慕哥6287543
TA贡献1831条经验 获得超10个赞
如果您正在使用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
提交
取消