我有一个函数可以检查文本是否在file.txt
中。
该函数的工作方式如下:如果文本包含在文件中,则文件将关闭。如果文本中未包含该文本,则会添加该文本。
但它不起作用。
import urllib2, re
from bs4 import BeautifulSoup as BS
def SaveToFile(fileToSave, textToSave):
datafile = file(fileToSave)
for line in datafile:
if textToSave in line:
datafile.close()
else:
datafile.write(textToSave + '\n')
datafile.close()
urls = ['url1', 'url2'] # i dont want to public the links.
patGetTitle = re.compile(r'<title>(.*)</title>')
for url in urls:
u = urllib2.urlopen(url)
webpage = u.read()
title = re.findall(patGetTitle, webpage)
SaveToFile('articles.txt', title)
# so here. If the title of the website is already in articles.txt
# the function should close the file.
# But if the title is not found in articles.txt the function should add it.
答案 0 :(得分:2)
您可以像这样更改SaveToFile
功能
您的title
是一个列表而不是字符串,因此您应该像SaveToFile('articles.txt', title[0])
一样调用它来获取列表的第一个元素
def SaveToFile(fileToSave, textToSave):
with open(fileToSave, "r+") as datafile:
for line in datafile:
if textToSave in line:
break
else:
datafile.write(textToSave + '\n')
注意:
<强>即。)强>
for i in []:
print i # This will print nothing since it is iterating over empty list same as yours
list
而不是string
,因为re.findall
返回一个列表对象,您必须将列表的第一个元素传递给该函数。for..else
,其他情况就可以了。<强>即。)强>
for i in []:
print i
else:
print "Nooooo"
输出
Nooooo
答案 1 :(得分:1)
你应该重构你的 SaveToFile 功能。
def SaveToFile(fileToSave, titleList):
with open(fileToSave, 'a+') as f:
data = f.read()
for titleText in titleList:
if titleText not in data:
f.write(titleText + '\n')
f.close()
此函数读取文件内容(如果存在或创建,如果不存在)并检查 textToSave 是否在文件内容中。如果找到 textToSave ,则关闭文件,否则将内容写入文件。
答案 2 :(得分:1)
只需使用r+
模式:
def SaveToFile(fileToSave, textToSave):
with open(fileToSave, 'r+') as datafile:
if textToSave not in datafile.read():
datafile.write(textToSave + '\n')
关于该文件模式,来自this answer:
``r+'' Open for reading and writing. The stream is positioned at the
beginning of the file.
并且re.find_all()
总是返回一个列表,所以如果你试图写一个列表而不是字符串,你就会得到一个错误。
所以你可以使用:
def SaveToFile(fileToSave, textToSave):
if len(textToSave) => 1:
textToSave = textToSave[0]
else:
return
with open(fileToSave, 'r+') as datafile:
if textToSave not in datafile.read():
datafile.write(textToSave + '\n')
答案 3 :(得分:0)
这似乎更接近你的问题。
这将检查文件中的文本:
def is_text_in_file(file_name, text):
with open(file_name) as fobj:
for line in fobj:
if text in line:
return True
return False
如果文件尚未存档,则使用上述功能检查并将文本写入文件的末尾。
def save_to_file(file_name, text):
if not is_text_in_file in (file_name, text):
with open(file_name, 'a') as fobj:
fobj.write(text + '\n')