My problem is following:
def searchWordlist():
path = str(raw_input(PATH))
word = str(raw_input(WORD))
with open(path) as f:
for line in f:
if word in line:
print "Word found"
Than I added following code:
else:
print "Word not found"
But this obviously can't work, because it will print "Word not found" until the word is found. Well.. but how can I print that the word is not found?! I srsly don't know.
Thank you in advance!
Python has a special trick for this kind of thing:
for line in f:
if word in line:
print "Word found"
break
else:
print "Word not found"
Here the else goes with the for, and specifically executes if the loop completes normally without hitting a break.
If all you want it to do is to print whether word is found in any of the lines:
def searchWordlist():
path = str(raw_input(PATH))
word = str(raw_input(WORD))
with open(path) as f:
if any(word in line for line in f):
print('Word found')
else:
print('Word not found')
def searchWordlist():
path = str(raw_input(PATH))
word = str(raw_input(WORD))
loc = -1
with open(path) as f:
for i, line in enumerate(f):
if word in line:
loc = i
break
if loc >= 0:
print ("Word found at line {}".format(loc))
else:
print ("Word not found")
As a bonus, this keeps track of where in the file the word in first seen, if at all.
You can get your function to return as soon as it finds the word if you only need to find the first occurrence. This has the added bonus of not having to loop through the whole file if it doesnt need to
def searchWordlist():
path = str(raw_input(PATH))
word = str(raw_input(WORD))
with open(path) as f:
for line in f:
if word in line:
print "Word found"
return 1
print "Word not found"
return 0
def searchWordlist():
found_word = False
path = str(raw_input(PATH))
word = str(raw_input(WORD))
with open(path) as f:
for line in f:
if word in line:
print "Word found"
found_word = True
if not found_word:
print "Word not found!"
What this code does is it executes your code as normal, except that if it finds the word it sets a boolean to true, signifying that it has found the word. It then, after completely parsing the file, sees if the word was found, if the variable is true. If the word was not found, then it prints that the word was not found. If you want the code to stop after the word is found for the first time, but break after found_word = True.
Related
I'm writing a program which takes the input string and prints out all anagrams of the string, from a text file, in a list.
So far, I've got the following; but nothing is returned and I don't quite understand why.
def anagrams( s1, s2 ):
s1_list = list(s1.lower())
s1_list.sort()
s2_list = list(s2.lower())
s2_list.sort()
if s1.lower() == s2.lower():
return(False)
elif s1_list == s2_list:
return(True)
else:
return(False)
def find_all_anagrams( string ):
anagrams_list = []
with open("english_words.txt", "r") as fileObject:
line = fileObject.readline()
if anagrams(string, line):
anagrams_list.append(line)
return anagrams_list
You need to iterate through all the words (I'm assuming one per line) in english_words.txt. You also need strip any newline characters from each word before you call anagrams().
def find_all_anagrams( string ):
anagrams_list = []
with open("english_words.txt", "r") as fileObject:
for line in fileObject: # check all lines
word = line.strip() # strip newline characters
if anagrams(string, word):
anagrams_list.append(word)
return anagrams_list # finally return results
I have been using python for some time, I have a file that is the english alphabet with each word on separate lines, and I have ironed out all the errors, but it doesn't type anything anymore. I'm open to any help, and improvements to the code, provided you explain how it works. If you are looking for the file it is here
f = open("C:\\my folders\\brit-a-z.txt", 'r')
print("Type in your cloze word like this: cl-z-")
def cloze(word):
for line in f:
match = True
if len(line) == len(word):
for i in range(len(word)):
if not word[i] == "-":
if line[i] == word[i]:
match == False
if match == True:
print(line)
while True:
cloze(input())
Once you've iterated through the file for the first word, the file closes. You'll need to save f. For example:
with open("C:\\my folders\\brit-a-z.txt", 'r') as f:
f = f.read().splitlines()
I want to check if a string is in a text file. If it is, do X. If it's not, do Y. However, this code always returns True for some reason. Can anyone see what is wrong?
def check():
datafile = file('example.txt')
found = False
for line in datafile:
if blabla in line:
found = True
break
check()
if True:
print "true"
else:
print "false"
The reason why you always got True has already been given, so I'll just offer another suggestion:
If your file is not too large, you can read it into a string, and just use that (easier and often faster than reading and checking line per line):
with open('example.txt') as f:
if 'blabla' in f.read():
print("true")
Another trick: you can alleviate the possible memory problems by using mmap.mmap() to create a "string-like" object that uses the underlying file (instead of reading the whole file in memory):
import mmap
with open('example.txt') as f:
s = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
if s.find('blabla') != -1:
print('true')
NOTE: in python 3, mmaps behave like bytearray objects rather than strings, so the subsequence you look for with find() has to be a bytes object rather than a string as well, eg. s.find(b'blabla'):
#!/usr/bin/env python3
import mmap
with open('example.txt', 'rb', 0) as file, \
mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as s:
if s.find(b'blabla') != -1:
print('true')
You could also use regular expressions on mmap e.g., case-insensitive search: if re.search(br'(?i)blabla', s):
As Jeffrey Said, you are not checking the value of check(). In addition, your check() function is not returning anything. Note the difference:
def check():
with open('example.txt') as f:
datafile = f.readlines()
found = False # This isn't really necessary
for line in datafile:
if blabla in line:
# found = True # Not necessary
return True
return False # Because you finished the search without finding
Then you can test the output of check():
if check():
print('True')
else:
print('False')
Here's another way to possibly answer your question using the find function which gives you a literal numerical value of where something truly is
open('file', 'r').read().find('')
in find write the word you want to find
and 'file' stands for your file name
if True:
print "true"
This always happens because True is always True.
You want something like this:
if check():
print "true"
else:
print "false"
Good luck!
I made a little function for this purpose. It searches for a word in the input file and then adds it to the output file.
def searcher(outf, inf, string):
with open(outf, 'a') as f1:
if string in open(inf).read():
f1.write(string)
outf is the output file
inf is the input file
string is of course, the desired string that you wish to find and add to outf.
Your check function should return the found boolean and use that to determine what to print.
def check():
datafile = file('example.txt')
found = False
for line in datafile:
if blabla in line:
found = True
break
return found
found = check()
if found:
print "true"
else:
print "false"
the second block could also be condensed to:
if check():
print "true"
else:
print "false"
Two problems:
Your function does not return anything; a function that does not explicitly return anything returns None (which is falsy)
True is always True - you are not checking the result of your function
.
def check(fname, txt):
with open(fname) as dataf:
return any(txt in line for line in dataf)
if check('example.txt', 'blabla'):
print "true"
else:
print "false"
How to search the text in the file and Returns an file path in which the word is found
(Как искать часть текста в файле и возвращять путь к файлу в котором это слово найдено)
import os
import re
class Searcher:
def __init__(self, path, query):
self.path = path
if self.path[-1] != '/':
self.path += '/'
self.path = self.path.replace('/', '\\')
self.query = query
self.searched = {}
def find(self):
for root, dirs, files in os.walk( self.path ):
for file in files:
if re.match(r'.*?\.txt$', file) is not None:
if root[-1] != '\\':
root += '\\'
f = open(root + file, 'rt')
txt = f.read()
f.close()
count = len( re.findall( self.query, txt ) )
if count > 0:
self.searched[root + file] = count
def getResults(self):
return self.searched
In Main()
# -*- coding: UTF-8 -*-
import sys
from search import Searcher
path = 'c:\\temp\\'
search = 'search string'
if __name__ == '__main__':
if len(sys.argv) == 3:
# создаем объект поисковика и передаем ему аргументы
Search = Searcher(sys.argv[1], sys.argv[2])
else:
Search = Searcher(path, search)
# начать поиск
Search.find()
# получаем результат
results = Search.getResults()
# выводим результат
print 'Found ', len(results), ' files:'
for file, count in results.items():
print 'File: ', file, ' Found entries:' , count
If user wants to search for the word in given text file.
fopen = open('logfile.txt',mode='r+')
fread = fopen.readlines()
x = input("Enter the search string: ")
for line in fread:
if x in line:
print(line)
found = False
def check():
datafile = file('example.txt')
for line in datafile:
if blabla in line:
found = True
break
return found
if check():
print "true"
else:
print "false"
found = False
def check():
datafile = file('example.txt')
for line in datafile:
if "blabla" in line:
found = True
break
return found
if check():
print "found"
else:
print "not found"
Here's another. Takes an absolute file path and a given string and passes it to word_find(), uses readlines() method on the given file within the enumerate() method which gives an iterable count as it traverses line by line, in the end giving you the line with the matching string, plus the given line number. Cheers.
def word_find(file, word):
with open(file, 'r') as target_file:
for num, line in enumerate(target_file.readlines(), 1):
if str(word) in line:
print(f'<Line {num}> {line}')
else:
print(f'> {word} not found.')
if __name__ == '__main__':
file_to_process = '/path/to/file'
string_to_find = input()
word_find(file_to_process, string_to_find)
"found" needs to be created as global variable in the function as "if else" statement is out of the function. You also don't need to use "break" to break the loop code.
The following should work to find out if the text file has desired string.
with open('text_text.txt') as f:
datafile = f.readlines()
def check():
global found
found = False
for line in datafile:
if 'the' in line:
found = True
check()
if found == True:
print("True")
else:
print("False")
The program is used to find anagrams for a inputted string. The possible anagrams come from a text file 'dict.txt'. However, I am trying to check if the inputted string is not in the dictionary. If the inputted string is not the dictionary, the program should not look for anagrams, and just print a message saying that the inputted string is not in the dictionary. Currently the code is saying that all of my inputted strings are not in the dictionary, which is incorrect.
def anagram(word,checkword):
for letter in word:
if letter in checkword:
checkword = checkword.replace(letter, '')
else:
return False
return True
def print_anagram_list():
if len(word_list) == 2:
print ('The anagrams for', inputted_word, 'are', (' and '.join(word_list)))
elif len(word_list) > 2:
print ('The anagrams for', inputted_word, 'are', (', '.join(word_list[:-1]))+ ' and ' +(word_list[-1]))
elif len(word_list) == 0:
print ('There are no anagrams for', inputted_word)
elif len(word_list) == 1:
print ('The only anagram for', inputted_word, 'is', (''.join(word_list)))
def anagram_finder():
for line in f:
word = line.strip()
if len(word)==len(inputted_word):
if word == inputted_word:
continue
elif anagram(word, inputted_word):
word_list.append(word)
print_anagram_list()
def check(wordcheck):
if wordcheck not in f:
print('The word', wordcheck, 'is not in the dictionary')
while True:
try:
f = open('dict.txt', 'r')
word_list=[]
inputted_word = input('Your word? ').lower()
check(inputted_word)
anagram_finder()
except EOFError:
break
except KeyboardInterrupt:
break
f.close()
Read all of your words into a list beforehand:
with open('dict.txt', 'r') as handle:
word_list = []
for line in handle:
word_list.append(line)
Replace for word in f with for word in word_list in your code, as now you have a list of all of the lines in the file.
Now, you can check if a word is in the list:
def check(wordcheck):
if wordcheck not in word_list:
print('The word', wordcheck, 'is not in the dictionary')
The with syntax lets you cleanly open a file without having to close it later one. Once you exit the scope of the with statement, the file automatically gets closed for you.
Also, you can simplify your anagram code a little bit:
def anagram(word, checkword):
return sorted(word) == sorted(checkword)
I want to check if a string is in a text file. If it is, do X. If it's not, do Y. However, this code always returns True for some reason. Can anyone see what is wrong?
def check():
datafile = file('example.txt')
found = False
for line in datafile:
if blabla in line:
found = True
break
check()
if True:
print "true"
else:
print "false"
The reason why you always got True has already been given, so I'll just offer another suggestion:
If your file is not too large, you can read it into a string, and just use that (easier and often faster than reading and checking line per line):
with open('example.txt') as f:
if 'blabla' in f.read():
print("true")
Another trick: you can alleviate the possible memory problems by using mmap.mmap() to create a "string-like" object that uses the underlying file (instead of reading the whole file in memory):
import mmap
with open('example.txt') as f:
s = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
if s.find('blabla') != -1:
print('true')
NOTE: in python 3, mmaps behave like bytearray objects rather than strings, so the subsequence you look for with find() has to be a bytes object rather than a string as well, eg. s.find(b'blabla'):
#!/usr/bin/env python3
import mmap
with open('example.txt', 'rb', 0) as file, \
mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as s:
if s.find(b'blabla') != -1:
print('true')
You could also use regular expressions on mmap e.g., case-insensitive search: if re.search(br'(?i)blabla', s):
As Jeffrey Said, you are not checking the value of check(). In addition, your check() function is not returning anything. Note the difference:
def check():
with open('example.txt') as f:
datafile = f.readlines()
found = False # This isn't really necessary
for line in datafile:
if blabla in line:
# found = True # Not necessary
return True
return False # Because you finished the search without finding
Then you can test the output of check():
if check():
print('True')
else:
print('False')
Here's another way to possibly answer your question using the find function which gives you a literal numerical value of where something truly is
open('file', 'r').read().find('')
in find write the word you want to find
and 'file' stands for your file name
if True:
print "true"
This always happens because True is always True.
You want something like this:
if check():
print "true"
else:
print "false"
Good luck!
I made a little function for this purpose. It searches for a word in the input file and then adds it to the output file.
def searcher(outf, inf, string):
with open(outf, 'a') as f1:
if string in open(inf).read():
f1.write(string)
outf is the output file
inf is the input file
string is of course, the desired string that you wish to find and add to outf.
Your check function should return the found boolean and use that to determine what to print.
def check():
datafile = file('example.txt')
found = False
for line in datafile:
if blabla in line:
found = True
break
return found
found = check()
if found:
print "true"
else:
print "false"
the second block could also be condensed to:
if check():
print "true"
else:
print "false"
Two problems:
Your function does not return anything; a function that does not explicitly return anything returns None (which is falsy)
True is always True - you are not checking the result of your function
.
def check(fname, txt):
with open(fname) as dataf:
return any(txt in line for line in dataf)
if check('example.txt', 'blabla'):
print "true"
else:
print "false"
How to search the text in the file and Returns an file path in which the word is found
(Как искать часть текста в файле и возвращять путь к файлу в котором это слово найдено)
import os
import re
class Searcher:
def __init__(self, path, query):
self.path = path
if self.path[-1] != '/':
self.path += '/'
self.path = self.path.replace('/', '\\')
self.query = query
self.searched = {}
def find(self):
for root, dirs, files in os.walk( self.path ):
for file in files:
if re.match(r'.*?\.txt$', file) is not None:
if root[-1] != '\\':
root += '\\'
f = open(root + file, 'rt')
txt = f.read()
f.close()
count = len( re.findall( self.query, txt ) )
if count > 0:
self.searched[root + file] = count
def getResults(self):
return self.searched
In Main()
# -*- coding: UTF-8 -*-
import sys
from search import Searcher
path = 'c:\\temp\\'
search = 'search string'
if __name__ == '__main__':
if len(sys.argv) == 3:
# создаем объект поисковика и передаем ему аргументы
Search = Searcher(sys.argv[1], sys.argv[2])
else:
Search = Searcher(path, search)
# начать поиск
Search.find()
# получаем результат
results = Search.getResults()
# выводим результат
print 'Found ', len(results), ' files:'
for file, count in results.items():
print 'File: ', file, ' Found entries:' , count
If user wants to search for the word in given text file.
fopen = open('logfile.txt',mode='r+')
fread = fopen.readlines()
x = input("Enter the search string: ")
for line in fread:
if x in line:
print(line)
found = False
def check():
datafile = file('example.txt')
for line in datafile:
if blabla in line:
found = True
break
return found
if check():
print "true"
else:
print "false"
found = False
def check():
datafile = file('example.txt')
for line in datafile:
if "blabla" in line:
found = True
break
return found
if check():
print "found"
else:
print "not found"
Here's another. Takes an absolute file path and a given string and passes it to word_find(), uses readlines() method on the given file within the enumerate() method which gives an iterable count as it traverses line by line, in the end giving you the line with the matching string, plus the given line number. Cheers.
def word_find(file, word):
with open(file, 'r') as target_file:
for num, line in enumerate(target_file.readlines(), 1):
if str(word) in line:
print(f'<Line {num}> {line}')
else:
print(f'> {word} not found.')
if __name__ == '__main__':
file_to_process = '/path/to/file'
string_to_find = input()
word_find(file_to_process, string_to_find)
"found" needs to be created as global variable in the function as "if else" statement is out of the function. You also don't need to use "break" to break the loop code.
The following should work to find out if the text file has desired string.
with open('text_text.txt') as f:
datafile = f.readlines()
def check():
global found
found = False
for line in datafile:
if 'the' in line:
found = True
check()
if found == True:
print("True")
else:
print("False")