大佬教程收集整理的这篇文章主要介绍了Python-将字符串打印到文本文件,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
text_file = open("Output.txt", "w")
text_file.write("Purchase amount: %s" % @R_421_10586@lamount)
text_file.close()
如果使用上下文管理器,则将自动为你关闭文件
with open("Output.txt", "w") as text_file:
text_file.write("Purchase amount: %s" % @R_421_10586@lamount)
如果你使用的是Python2.6或更高版本,则最好使用 str.format()
with open("Output.txt", "w") as text_file:
text_file.write("Purchase amount: {0}".format(@R_421_10586@lamount))
在python3中,file
该print
函数有一个可选参数
with open("Output.txt", "w") as text_file:
print("Purchase amount: {}".format(@R_421_10586@lamount), file=text_filE)
python3.6引入了f字符串作为另一种选择
with open("Output.txt", "w") as text_file:
print(f"Purchase amount: {@R_421_10586@lamount}", file=text_filE)
以上是大佬教程为你收集整理的Python-将字符串打印到文本文件全部内容,希望文章能够帮你解决Python-将字符串打印到文本文件所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。