Python - 如何检查文本是否在文件 txt 中?
Python - How to check if the text is in a file txt?
我有一个函数可以检查文本是否在 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.
这似乎更接近你的问题。
这会检查文件中的文本是否:
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')
你应该重构你的 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 则关闭文件,否则将内容写入文件。
像这样使用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()
总是return一个列表,所以如果你试图写一个列表而不是字符串,你会得到一个错误。
所以你可以使用:
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')
您可以像这样更改 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
returns 列表对象必须将列表的第一个元素传递给函数。
- 我在这里使用了
for..else
如果循环没有正确终止,其他情况将起作用。
即)
for i in []:
print i
else:
print "Nooooo"
输出:
Nooooo
我有一个函数可以检查文本是否在 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.
这似乎更接近你的问题。
这会检查文件中的文本是否:
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')
你应该重构你的 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 则关闭文件,否则将内容写入文件。
像这样使用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()
总是return一个列表,所以如果你试图写一个列表而不是字符串,你会得到一个错误。
所以你可以使用:
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')
您可以像这样更改 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
returns 列表对象必须将列表的第一个元素传递给函数。 - 我在这里使用了
for..else
如果循环没有正确终止,其他情况将起作用。
即)
for i in []:
print i
else:
print "Nooooo"
输出:
Nooooo