簡體   English   中英

如何在python中將字符串寫入/打印到新的文本文件?

[英]How to write/print a string to a new textfile in python?

我在變量中有一個字符串,我想將該變量以打印形式IE寫入文本文件。我的明顯猜測是這樣做的:

'filename'.write(string_variable)

但是它給了我這個錯誤。

Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
main('studyguide.txt','decrypted.txt')
File "C:/Users/Casey/Documents/New folder/pa11.py", line 45, in main
writefile.write(finalstr)
AttributeError: 'str' object has no attribute 'write'

有任何想法嗎?

您需要open一個文件,並使用file.write

with open(writefile, 'w') as f:
    f.write(finalstr)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM