![](/img/trans.png)
[英]How can I assign the value from the slider to a variable? I can only use .get() to print the output, not store it
[英]How can I store an output print data?
我的腳本從指定的 redditor 打印前 5 個子版塊。
reddit = praw.Reddit(
client_id="",
client_secret="",
password="",
user_agent="",
username="",
)
for submission in reddit.redditor("").top(limit=5):
print(submission.subreddit)
我試圖將正在打印的內容存儲在 txt 文件中。
我試圖使用這種方法:
f = open('file.txt', 'w+')
f.write(submission.subreddit)
f.close()
但最后收到此錯誤
TypeError: write() 參數必須是 str,而不是 Subreddit
任何想法如何將子目錄存儲在 txt 文件中?
當您將某些內容寫入文件,但該內容不是字符串時,您需要先將其轉換為字符串。
str_subreddit = str(submission.subreddit)
但是,有更好的方法來做你想做的事情:
python my_script.py > file.txt
。
>>
print
接受一個file
參數: print(str_subreddit, file=f)
此外,如果您嘗試將 append 轉換為具有f = open('file.txt', 'w+')
的文件,則需要a
not w+
。 w+
是另外一回事。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.