text_file = open("Output.txt", "w")
text_file.write("Purchase Amount: " 'TotalAmount')
text_file.close()
I want to 대체하십시오 문자열으로 변수 값을 텍스트 문서를 토탈라마운트 '에'. 누가 제발 알려주세요 수 how to do this?
text_file = open("Output.txt", "w")
text_file.write("Purchase Amount: %s" % TotalAmount)
text_file.close()
이 파일은 자동으로 당신꺼에요 단힌 컨텍스트로 manager 를 사용하는 경우
with open("Output.txt", "w") as text_file:
text_file.write("Purchase Amount: %s" % TotalAmount)
with open("Output.txt", "w") as text_file:
text_file.write("Purchase Amount: {0}".format(TotalAmount))
'대한' 대신 ' {0} {} python2.7 이상 사용할 수 있습니다'
Python3 좁히어 선택적입니다 '에서', '기능은 파일' 매개 변수를 인쇄하십시오.
with open("Output.txt", "w") as text_file:
print("Purchase Amount: {}".format(TotalAmount), file=text_file)
[F 문장열] () 는 python3.6 https://docs.python.org/3/whatsnew/3.6.html # pep 498 포맷됩니다 리터럴 문자열) 의 또 다른 대체
with open("Output.txt", "w") as text_file:
print(f"Purchase Amount: {TotalAmount}", file=text_file)
전달할 경우 여러 인수의 튜플 사용할 수 있습니다
price = 33.3
with open("Output.txt", "w") as text_file:
text_file.write("Purchase Amount: %s price %f" % (TotalAmount, price))
자세한 내용: https://stackoverflow.com/questions/15286401/print-multiple-arguments-in-python
>. Python3 사용하는 경우.
[Print 함수] [1] 사용하면 됩니다.
your_data = {"Purchase Amount": 'TotalAmount'}
print(your_data, file=open('D:\log.txt', 'w'))
>. Python2 대한
이것은 예를 파이썬 인쇄하십시오 구체화하십시오 텍스트 파일로
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()
[1]: https://docs.python.org/3/library/functions.html # 인쇄하십시오.
import pathlib
pathlib.Path("output.txt").write_text("Purchase Amount: {}" .format(TotalAmount))
Vmware., 파이썬 3.6tb f 문장열 사용할 수 있습니다.
pathlib.Path("output.txt").write_text(f"Purchase Amount: {TotalAmount}")