I'm designing a program that looks through a list of words, and counts how many words only have the letters p, y, t, h, o and n in them.
So far, my code is:
def find_python(string, python):
"""searches for the letters 'python' in the word."""
for eachLetter in python:
if eachLetter not in string:
return False
return True
def main():
python = 'python'
how_many = 0
try:
fin = open('words.txt')#open the file
except:
print("No, no, file no here") #if file is not found
for eachLine in fin:
string = eachLine
find_python(string, python)
if find_python(string, python) == True:
how_many = how_many + 1#increment count if word found
print how_many#print out count
fin.close()#close the file
if __name__ == '__main__':
main()
However, my code is returning the incorrect number of words, for example, it will return the word 'xylophonist' if I put in the print statement for it because it has the letters python in it. What should I do so it will reject any word that has forbidden letters?
Correct your test function:
def find_python(string, python):
"""searches for the letters 'python' in the word.
return True, if string contains only letters from python.
"""
for eachLetter in string:
if eachLetter not in python:
return False
return True
Welcome to regular expressions:
import re
line = "hello python said the xylophonist in the ythoonp"
words = re.findall(r'\b[python]+\b',line)
print words
returns
['python', 'ythoonp']
If what you want is to find how many times the actual word python appears, then you should issue a re.findall(r'\bpython\b')
If you don't want to go this route, I suggest you return false if any of the letters of the string is NOT p,y,t,h,o or n.
from os import listdir
def diagy(letters,li):
return sum( any(c in letters for c in word) for word in li )
def main():
dir_search = 'the_dir_in_which\\to_find\\the_file\\'
filename = 'words.txt'
if filename in listdir(dir_search):
with open(dir_search + 'words.txt',) as f:
li = f.read().split()
for what in ('pythona','pyth','py','ame'):
print '%s %d' % (what, diagy(what,li))
else:
print("No, no, filename %r is not in %s" % (filename,dir_search))
if __name__ == '__main__':
main()
Related
i learn python and i do a discord bot. I have some difficulties to print the element after "anivia". i cant say if there is "anivia" in the 'texte' and i can count him but i don't know how to print the element after "anivia", if someone can help me please :)
#bot.command()
async def counter(ctx, *champion):
champion = " ".join(champion)
url = "https://u.gg/lol/champions/"
counter = "/counter"
uurl = url + champion + counter
await ctx.send(uurl)
import urllib.request
with urllib.request.urlopen(uurl) as response:
texte = response.read()
if ("anivia" in str(texte)):
print("Le mot existe !")
else:
print("Le mot n'existe pas!")
test = str(texte)
z = test.count('anivia')
print(z)
I can count 9 "anivia" with z and i want to print the next element after all the anivia (example: " hello i m anivia and i like anivia test": and, test).
Thanks for your help :)
If you're familiar with regular expressions (regex), this becomes very simple:
import re
# This pattern will capture the first word that comes after "anivia"
pattern = r'anivia (\w+)'
# Using this string as our example input
example_string = "anivia first anivia second and finally anivia third"
results = re.findall(pattern, example_string)
print(results) # Output: ['first', 'second', 'third']
Here is an approach that uses an auxiliary variable to mark when the next word needs to be printed.
test_string = "Hello, I am anivia on mid or anivia jungle"
do_print = False
splitted = test_string.split()
for word in splitted:
if do_print:
do_print = False
print(word)
if word == "anivia":
do_print = True
Output:
on
jungle
yeah, the those solution works with strings (i tried too with regex) but
do_print = False
splitted = test_string.split()
for word in splitted:
# print(word)
if do_print:
do_print = False
if word == "anivia":
do_print = True
test_string = str(texte)
do_print = False
splitted = test_string.split()
for word in splitted:
# print(word)
if do_print:
do_print = False
# print(word)
if word == "champion_id":
do_print = True``
on the first case i have the ("on" and the "jungle") but with my str(texte), that's doesn't fonction :S.
If someone knows why, the 2 test_strings are "strings"
^^ ty for your answers :)
I have to count the number of times a given word appears in a given text file, this one being the Gettysburg Address. For some reason, it is not counting my input of 'nation' so the output looks as such:
'nation' is found 0 times in the file gettysburg.txt
Here is the code I have currently, could someone point out what I am doing incorrectly?
fname = input("Enter a file name to process:")
find = input("Enter a word to search for:")
text = open(fname, 'r').read()
def processone():
if text is not None:
words = text.lower().split()
return words
else:
return None
def count_word(tokens, token):
count = 0
for element in tokens:
word = element.replace(",", " ")
word = word.replace("."," ")
if word == token:
count += 1
return count
words = processone()
word = find
frequency = count_word(words, word)
print("'"+find+"'", "is found", str(frequency), "times in the file", fname)
My first function splits the file into a string and turns all letters in it lower case. The second one removes the punctuation and is supposed to count the word given in the input.
Taking my first coding class, if you see more flaws in my coding or improvements that could be made, as well as helping find the solution to my problem, feel free.
In the for loop in the count_word() function, you have a return statement at the end of the loop, which exits the function immediately, after only one loop iteration.
You probably want to move the return statement to be outside of the for loop.
as a starter I would suggest you to use print statements and see what variables are printing, that helps to breakdown the problem. For example, print word was showing only first word from the file, which would have explained the problem in your code.
def count_word(tokens, token):
count = 0
for element in tokens:
word = element.replace(",", " ")
word = word.replace("."," ")
print (word)
if word == token:
count += 1
return count
Enter a file name to process:gettysburg.txt
Enter a word to search for:nation
fourscore
'nation' is found 0 times in the file gettysburg.txt
Use code below:
fname = input("Enter a file name to process:")
find = input("Enter a word to search for:")
text = open(fname, 'r').read()
def processone():
if text is not None:
words = text.lower().split()
return words
else:
return None
def count_word(tokens, token):
count = 0
for element in tokens:
word = element.replace(",", " ")
word = word.replace("."," ")
if word == token:
count += 1
return count
words = processone()
word = find
frequency = count_word(words, word)
print("'"+find+"'", "is found", str(frequency), "times in the file", fname)
statement "return" go out statement "for"
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")
I'm trying to get my program to go through an input sentence (e.g. "hello there!")
and see if any of the words in the input are in a list.
here is the code so far:
def findWholeWord(w):
return re.compile(r'\b({0})\b'.format(w), flags=re.IGNORECASE).search
i.upper() #i is the inputted variable as a string
WordsDocument = open('WordsDocument.txt').readlines()
for words in WordsDocument:
WordsList.append(words)
for word in i:
if findWholeWord(word) in WordsList:
print("Word Match")
Can someone help me develop a better solution / fix this so it works?
import re
def findWholeWord(w): # input string w
match_list = [] # list containing matched words
input_list = w.split(" ")
file = open('WordsDocument.txt', 'r')
text = file.read().lower()
file.close()
text = re.sub('[^a-z\ \']+', " ", text)
words_list = list(text.split())
for word in input_list:
if word in words_list:
print("Word Found: " + str(word))
match_list.append(word)
return match_list
def anagram(word,check):
for letter in word:
if letter in check:
check = check.replace(letter, '')
else:
return 0
return 1
while True:
f = open('dictionary.txt', 'r')
try:
user_input = input('Word? ')
for word in f:
word = word.strip()
if len(word)==len(user_input):
if word == user_input:
continue
elif anagram(word, input):
print (word)
#try:
#if word == 1:
#print ('The only anagram for', user_input, 'is', word)
#elif word > 1:
#print ('The anagrams for', user_input, 'are', word)
#except TypeError:
#pass
except EOFError:
break
f.close()
The function works as I want it to, but I need some help with the output. I want the output in one line, and the wording should reflect the amount of anagrams found. (i.e. 'only one anagram', 'the anagrams are', 'there are no anagrams', or 'the word is not in the dictionary') The comments in the code are what I have tried. Thanks for any help.
The way I understand your program, you want to continuously prompt the user for words until he presses Ctrl-D (which results in an EOF error and breaks the loop)? In that case, you should read the file only once, before the beginning of the loop, and construct a list or set of the words in it. Also, your try/except statement should only contain the call to input as this is the only place where this exception can occur in your function.
Now on to your main question - to count the number of results and print different statements accordingly, just use a list comprehension to get a list of all anagrams of the input. Then you can count the anagrams and join them together to form an output string.
def find_anagrams():
with open("dictionary.txt", "r") as fileInput:
words = set(word.strip() for word in fileInput)
while True:
try:
user_input = input("Word? ").strip()
except:
break #you probably don't care for the type of exception here
anagrams = [word for word in words if anagram(word, user_input)]
print_results(anagrams)
def print_results(anagrams):
if len(anagrams) == 0:
print("there are no anagrams")
elif len(anagrams) == 1:
print("the only anagram is %s" % anagrams[0])
else:
print("there are %s anagrams: %s" % (len(anagrams), ', '.join(anagrams)))
The only thing missing from this code is cheking that the input word is not a part of the result list, but this can be moved to the anagram function. The function can also be simplified using the Counter class from the built-in collections module. This class is a dictionary-like object that can be constructed from an iterable and maps each object in the iterable to the number of its occurrences:
>>> Counter("hello") == {"h":1, "e":1, "l":2, "o": 1}
True
So we can rewrite the anagram function like this:
from collections import Counter
def anagram(word, check):
return not word == check and Counter(word) == Counter(check)
you can create a list with your results like this:
with open("dictionary.txt", "r") as fileInput:
user_input = input("Search keyword: ").strip()
listAnagrams = []
for line in fileInput.readlines():
for word in line.split(" "):
if len(word) == len(user_input):
if word == user_input:
continue
elif anagram(word, user_input):
listAnagrams.append(word)
if len(listAnagrams) == 1:
print ('The only anagram for', user_input, 'is', listAnagrams[0])
elif len(listAnagrams) > 1:
print ('The anagrams for', user_input, 'are', ", ".join(listAnagrams))
else:
print ('No anagrams found for', user_input)