检查文本文件的内容,如果不存在则添加内容
Check content of text file and add content if it's not there
我是 python 的新手。我正在使用 python 设计一个报价应用程序。我使用 BeautifulSoup 从聪明的报价网站获取当天的报价。我会将其附加到文本文件中。在这里,如果当天的报价已经添加,当我再次执行程序时,它应该跳过它。如何让它成为可能
代码如下:
from bs4 import BeautifulSoup
import socket
import requests
import subprocess
import datetime
def quotenotify():
timestamp = datetime.datetime.now().strftime("%b %d")
res = requests.get('https://www.brainyquote.com/quote_of_the_day')
soup = BeautifulSoup(res.text, 'lxml')
image_quote = soup.find('img', {'class': 'p-qotd bqPhotoDefault bqPhotoDefaultFw img-responsive delayedPhotoLoad'})
quoteday=image_quote['alt']
text_file = open("quotes.log", "a+")
text_file.write("%s"%timestamp+"\t"+"%s"% quoteday)
text_file.write("\n")
text_file.close()
return
quotenotify()
在文件中输出:
Mar 29 Where there is a great love, there are always wishes. - Willa Cather
Mar 29 Where there is great love, there are always wishes. - Willa Cather
继续评论:
from bs4 import BeautifulSoup
import requests
import datetime
def quotenotify():
timestamp = datetime.datetime.now().strftime("%b %d")
res = requests.get('https://www.brainyquote.com/quote_of_the_day')
soup = BeautifulSoup(res.text, 'lxml')
image_quote = soup.find('img', {'class': 'p-qotd bqPhotoDefault bqPhotoDefaultFw img-responsive delayedPhotoLoad'})['alt']
with open("quotes.log", "w+") as f:
if image_quote not in f.read():
f.write("%s"%timestamp+"\t"+"%s"% image_quote + "\n")
quotenotify()
编辑:
由于使用模式 w+
会截断文件,我建议使用 pathlib:
from bs4 import BeautifulSoup
import requests
import datetime
from pathlib import Path
def quotenotify():
timestamp = datetime.datetime.now().strftime("%b %d")
res = requests.get('https://www.brainyquote.com/quote_of_the_day')
soup = BeautifulSoup(res.text, 'lxml')
image_quote = timestamp + "\t" + soup.find('img', {'class': 'p-qotd bqPhotoDefault bqPhotoDefaultFw img-responsive delayedPhotoLoad'})['alt']
with open("quotes3.log", "a+") as f:
contents = [Path("quotes3.log").read_text()]
print(contents)
print(image_quote)
if image_quote not in contents:
f.write("%s" % timestamp + "\t" + "%s" % image_quote + "\n")
quotenotify()
如@DirtyBit 所述,您应该先以读取模式打开文件并将内容加载到变量中。
您可以在下面的示例中看到,我将内容加载到变量中,然后仅当变量不在文本文件中时才附加到文件中。
text_file = open('test-file.txt', 'r+')
read_the_file = text_file.read()
text_file.close()
text_file = open('test-file.txt', 'a+')
new_string = 'Smack Alpha learns python'
if new_string not in read_the_file:
text_file.write(new_string + '\n')
text_file.close()
我是 python 的新手。我正在使用 python 设计一个报价应用程序。我使用 BeautifulSoup 从聪明的报价网站获取当天的报价。我会将其附加到文本文件中。在这里,如果当天的报价已经添加,当我再次执行程序时,它应该跳过它。如何让它成为可能
代码如下:
from bs4 import BeautifulSoup
import socket
import requests
import subprocess
import datetime
def quotenotify():
timestamp = datetime.datetime.now().strftime("%b %d")
res = requests.get('https://www.brainyquote.com/quote_of_the_day')
soup = BeautifulSoup(res.text, 'lxml')
image_quote = soup.find('img', {'class': 'p-qotd bqPhotoDefault bqPhotoDefaultFw img-responsive delayedPhotoLoad'})
quoteday=image_quote['alt']
text_file = open("quotes.log", "a+")
text_file.write("%s"%timestamp+"\t"+"%s"% quoteday)
text_file.write("\n")
text_file.close()
return
quotenotify()
在文件中输出:
Mar 29 Where there is a great love, there are always wishes. - Willa Cather
Mar 29 Where there is great love, there are always wishes. - Willa Cather
继续评论:
from bs4 import BeautifulSoup
import requests
import datetime
def quotenotify():
timestamp = datetime.datetime.now().strftime("%b %d")
res = requests.get('https://www.brainyquote.com/quote_of_the_day')
soup = BeautifulSoup(res.text, 'lxml')
image_quote = soup.find('img', {'class': 'p-qotd bqPhotoDefault bqPhotoDefaultFw img-responsive delayedPhotoLoad'})['alt']
with open("quotes.log", "w+") as f:
if image_quote not in f.read():
f.write("%s"%timestamp+"\t"+"%s"% image_quote + "\n")
quotenotify()
编辑:
由于使用模式 w+
会截断文件,我建议使用 pathlib:
from bs4 import BeautifulSoup
import requests
import datetime
from pathlib import Path
def quotenotify():
timestamp = datetime.datetime.now().strftime("%b %d")
res = requests.get('https://www.brainyquote.com/quote_of_the_day')
soup = BeautifulSoup(res.text, 'lxml')
image_quote = timestamp + "\t" + soup.find('img', {'class': 'p-qotd bqPhotoDefault bqPhotoDefaultFw img-responsive delayedPhotoLoad'})['alt']
with open("quotes3.log", "a+") as f:
contents = [Path("quotes3.log").read_text()]
print(contents)
print(image_quote)
if image_quote not in contents:
f.write("%s" % timestamp + "\t" + "%s" % image_quote + "\n")
quotenotify()
如@DirtyBit 所述,您应该先以读取模式打开文件并将内容加载到变量中。
您可以在下面的示例中看到,我将内容加载到变量中,然后仅当变量不在文本文件中时才附加到文件中。
text_file = open('test-file.txt', 'r+')
read_the_file = text_file.read()
text_file.close()
text_file = open('test-file.txt', 'a+')
new_string = 'Smack Alpha learns python'
if new_string not in read_the_file:
text_file.write(new_string + '\n')
text_file.close()