Checking if an item is in a set in Python? - python

Writing a program to check if a word that the user inputs is in a pre-existing set; no matter what word I input the program returns "False" (even when it's a word I know is in the set). Code is below:
name = input("which file would you like to open? ")
s=input("word? ")
F = open(name, "r")
words = set()
words.add(F)
def contains(s,words):
if s in words:
print("true")
else:
print("false")
contains(s,words)

Assuming there is one word per line in the file, e.g.
asd
asdf
You can use this, which adds every line to words:
name = input("which file would you like to open? ")
s = input("word? ")
F = open(name, "r")
words = set()
for line in F: # add every line to words (assuming one word per line)
words.add(line.strip())
def contains(s, words):
if s in words:
print("true")
else:
print("false")
contains(s, words)
Printing an output of:
which file would you like to open? file.txt
word? asd
true
Edit: a much shorter way for the actual task:
name = input("which file would you like to open? ")
s = input("word? ")
F = open(name, "r")
print("true") if s in F.read() else print("false")

Assuming your file looks like this:
banana
apple
apple
orange
Let's create that file:
with open("test.txt","w") as f:
f.write("banana\napple\napple\norange")
Now let's run a sample code:
s= input("word? ")
words = set()
# Using with we don't need to close the file
with open("test.txt") as f:
# here is the difference from your code
for item in f.read().split("\n"):
words.add(item)
def contains(s,words):
for word in words:
if s in word:
print("true")
break
else:
print("false")
contains(s,words)
Typing:
apple returns "true"
ap returns "true"
oeoe returns "false"

The right way is to use a generator for this:
name = input("which file would you like to open? ")
word_to_look_for=input("word? ")
def check(word_to_look_for, word_from_file):
return word_to_look_for == word_from_file
with open(name, "r") as file:
# The code inside the parentheses () returns a generator object
word_exists = (check(word_to_look_for, word_from_file.rstrip("\n")) for word_from_file in file.readlines())
# This will return true if either one of the "check" function results is True
print(any(word_exists))

For first, it's better to open files using
with open(filepath, 'r') as input_file:
you could read about this here
https://docs.python.org/3/tutorial/inputoutput.html
Also you try to add file into set, but you need to add words.
So this is working (and more pythonic) code:
import os
def load_data(filepath):
if not os.path.exists(filepath):
return None
with open(filepath, 'r') as input_file:
text = input_file.read()
return text
if __name__ == '__main__':
filepath = input("Which file would you like to open?")
word=input("What we want to find?")
text = load_data(filepath)
if not text:
print("File is empty or not exists!")
raise SystemExit
words = set(text.split())
print(word in words)

There are a few things going on here which I'm not sure you're totally clear on:
First, F is a file. I'm going to guess that you're intention here is that you're trying to check whether a word is in a file of words (like a dictionary). To do this however you'll need to do something like this:
filename = "somefilename.txt"
words = set()
# open the file
file = open(filename, "r")
# read the lines into an array
lines = file.readlines()
for line in lines:
words.add(line)
testWord = input("word? ")
The second issue is that you're using a function but you're mistaking your parameters as the same variables you've declared in your main flow. Eg.
# this is a verbose way of doing this
words = set()
testWord = input()
def isWordInSet(word, words):
# words here is not the same as the words
# outside the function
if word in words:
print("True")
else:
print("False")
isWordInSet(testWord, words)

Related

How to check if a word is in a list that is in another function?

To put it simply, I was given a .txt file with a list of words in it, then asked to read it in python and store it in a list with a function. So far, no problem. I wrote something that looks like this:
def load_lexicon():
with open("lexicon.txt", "r") as f:
content_list = f.read().splitlines()
lexique = open("lexicon.txt", "r")
liste_des_mots = []
for i in lexique:
ligne = i.strip()
liste = ligne.split()
liste_des_mots.append(liste)
return liste_des_mots
The problem is that I would like to create a function that accesses this list that I created in the previous function and checks if a given word is there or not.
I tried something like this:
def check_word():
search_word = input("enter the word you want to search for: ")
if search_word in load_lexicon():
print ('word found')
else:
print ('word not found')
But the thing is, no matter what word I put in, whether it's in the .txt file or not it always returns that the word is not in it, for example:
enter the word you want to search for: 'a'
word not found
or
enter the word you want to search for: a
word not found
just so you know, a is in the .txt file
First you open the file and read the contents as content_list then you make another file handle called lexique. So you never actually do anything with the file contents. Additionally as #quamrana mentions you would need to either iterate through each word on each line or use extends to add them to your list otherwise that list is a list of lines which is ironically exactly what content_list was before that for loop.
def load_lexicon():
with open("lexicon.txt", "r") as f:
content_list = f.readlines()
liste_des_mots = []
for i in content_list:
ligne = i.strip()
liste = ligne.split()
liste_des_mots.extend(liste)
return liste_des_mots
def load_lexicon():
with open("lexicon.txt", "r") as f:
content_list = f.read().splitlines()
global liste_des_mots
liste_des_mots = []
lexique = open("lexicon.txt", "r")
for i in lexique:
ligne = i.strip()
#print(ligne)
liste = ligne.split()
liste_des_mots.append(liste)
return liste_des_mots
#print(liste_des_mots)
count =len(liste_des_mots)
def check_word():
search_word = input("enter the word you want to search for: ")
for i in range(count):
#print(liste_des_mots[i])
if search_word in liste_des_mots[i]:
print('word found')
break
else:
print('word not found')
check_word()
in your code you must focus on this line : if search_word in load_lexicon():
This is most likely a nested list. For this reason, it cannot make the call correctly. Once again a iteration is required within the list.
I tried to fix it. I hope it helps

stop while loop when the text ends

I have a program that loops through the lines of a book to match some tags I've created indicating the start and the end of each chapter of this book. I want to separate each chapter into a different file. The program finds each chapter and asks the user to name the file, then it continues until the next chapter and so on. I don't know exactly where to put my "break" or something that could stop my loop. The program runs well but when it reaches the last chapter it goes back to the first chapter. I want to stop the loop and terminate the program when the tags and the chapters finish and also print something like "End of chapters". Can anyone help me with that? The code is below:
import re
def separate_files ():
with open('sample.txt') as file:
chapters = file.readlines()
pat=re.compile(r"[#introS\].[\#introEnd#]")
reg= list(filter(pat.match, chapters))
txt=' '
while True:
for i in chapters:
if i in reg:
print(i)
inp=input("write text a file? Y|N: ")
if inp =='Y':
txt=i
file_name=input('Name your file: ')
out_file=open(file_name,'w')
out_file.write(txt)
out_file.close()
print('text', inp, 'written to a file')
elif inp =='N':
break
else:
continue
else:
continue
separate_files()
I think a simpler definition would be
import re
def separate_files ():
pat = re.compile(r"[#introS\].[\#introEnd#]")
with open('sample.txt') as file:
for i in filter(pat.match, file):
print(i)
inp = input("write text to a file? Y|N: ")
if inp != "Y":
continue
file_name = input("Name of your file: ")
with open(file_name, "w") as out_file:
out_file.write(i)
print("text {} written to a file".format(i))
Continue the loop as soon as possible in each case, so that the following code doesn't need to be nested more and more deeply. Also, there's no apparent need to read the entire file into memory at once; just match each line against the pattern as it comes up.
You might also consider simply asking for a file name, treating a blank file name as declining to write the line to a file.
for i in filter(pat.match, file):
print(i)
file_name = input("Enter a file name to write to (or leave blank to continue: ")
if not file_name:
continue
with open(file_name, "w") as out_file:
out_file.write(i)
print("text {} written to {}".format(i, file_name)
I can't run your code but I assume if you remove the
while True:
line it should work fine. This will always be executed as there is nothing checked

Searching or a specific substring in a file

This question is possibly a duplicate but any answers i find don't seem to work. I have a .txt file full of this layout:
artist - song, www.link.com
artist2 - song2, www.link2.com
This is my general purpose:
uinput = input("input here: ")
save = open("save.txt", "w+")
ncount = save.count("\n")
for i in range(0, ncount):
t = save.readline()
if uinput in t:
print("Your string " uinput, " was found in" end = "")
print(t)
My intention is: If the userinput word was found in a line then print the entire line or the link.
You want to read the file, but you are opening the file in write mode. You should use r, not w+
The simplest way to iterate over a file is to have a for loop iterating directly over the file object
Not an error but a nitpick. You do not close your file. You can remedy this with with.. as context manager
uinput = input("input here: ")
with open("save.txt", "r") as f:
for line in f:
if uinput in line:
print('Match found')
You can use list-comprehension to read the file and get only the lines that contain the word, for example:
with open('save.txt', 'r') as f:
uinput = input("input here: ")
found = [line.rstrip() for line in f if uinput.lower() in line.lower()]
if found:
print('Found in these lines: ')
print('\n'.join(found))
else:
print('Not found.')
If you want to print the link only, you can use:
found = [line.rstrip().split(',')[1] for line in f if uinput.lower() in line.lower()]
You can use list comprehension to fetch the lines containing user input words.
use below code:
try:
f = open("file/toyourpath/filename.txt", "r")
data_input = raw_input("Enter your listed song from file :");
print data_input
fetch_line = [line for line in f if data_input in line]
print fetch_line
f.close()
except ValueError, e:
print e

Printing results to text file

I am wanting to print the results shown in my console from the loop below into a text file. I have tried putting this code in the loop as seen in the example:
f = open('out.txt', 'w',)
sys.stdout = f
However when this is in the loop I only get one set of results instead of the full expected.
wordlist = input("What is your word list called?")
f = open(wordlist)
l = set(w.strip().lower() for w in f)
chatlog = input("What is your chat log called?")
with open(chatlog) as f:
found = False
for line in f:
line = line.lower()
if any(w in line for w in l):
print (l)
print(line)
found = True
f = open('out.txt', 'w',)
sys.stdout = f
if not found:
print("not here")
You should use write() function to write your result into the file.
Code should be something as:
wordlist = input("What is your word list called?")
f = open(wordlist)
l = set(w.strip().lower() for w in f)
chatlog = input("What is your chat log called?")
with open(chatlog) as f:
found = False
file = open("out.txt", "w")
for line in f:
line = line.lower()
if any(w in line for w in l):
found = True
file.write(line)
if not found:
print("not here")
You should make sure that you open the 'out.txt' for writing outside the loop, not inside the loop
If you want to write text to a file, you should use the .write() method of the File object, as specified in https://docs.python.org/2/tutorial/inputoutput.html, rather than the print method.
The problem is, each iteration you open it with write mode
Due to python documentation:
'w' for writing (truncating the file if it already exists)
for your purposes you should use "a" mode (append) or open file out of cycle

Why my function is returning empty string in python?

What I am doing is, removing all parts of speech except nouns from a text.
I have written a function for that. It may not be the best or optimized code to do that because I have just started coding in python. I am sure the bug must be very basic but I am just not able to figure it out.
In my function two inputs go as parameters. One is the location of text on hard drive and other is the location of file where we want the output.
Following is the code.
def extract_nouns(i_location, o_location):
import nltk
with open(i_location, "r") as myfile:
data = myfile.read().replace('\n', '')
tokens = nltk.word_tokenize(data)
tagged = nltk.pos_tag(tokens)
length = len(tagged)
a = list()
for i in range(0,length):
print(i)
log = (tagged[i][1][0] == 'N')
if log == False:
a.append(tagged[i][0])
fin = open(i_location, 'r')
fout = open(o_location, "w+")
for line in fin:
for word in a:
line = line.replace(word, "")
fout.write(line)
with open(o_location, "r") as myfile_new:
data_out = myfile_new.read().replace('\n', '')
return data_out
When I call this function it is working just fine. I am getting the output on hard disk as I had intended but it does not return the output on the interface or should I say, it is returning a blank string instead of the actual output string.
This is how I am calling it.
t = extract_nouns("input.txt","output.txt")
If you want to try it, take following as the content of input file
"At eight o'clock on
Thursday film morning word line test
best beautiful Ram Aaron design"
This is the output I am getting in the output file (output.txt) when I call the function but the function returns blank string on the interface instead. It does not even print the output.
"
Thursday film morning word line test
Ram Aar design"
You need to close the file first:
for line in fin:
for word in a:
line = line.replace(word, "")
fout.write(line)
fout.close()
Using with is usually the best way to open files as it automatically closes them and file.seek() to go back to the start of the file to read :
def extract_nouns(i_location, o_location):
import nltk
with open(i_location, "r") as myfile:
data = myfile.read().replace('\n', '')
tokens = nltk.word_tokenize(data)
tagged = nltk.pos_tag(tokens)
length = len(tagged)
a = []
for i in range(0,length):
print(i)
log = (tagged[i][1][0] == 'N')
if not log:
a.append(tagged[i][0])
with open(i_location, 'r') as fin, open(o_location, "w+") as fout:
for line in fin:
for word in a:
line = line.replace(word, "")
fout.write(line)
fout.seek(0) # go back to start of file
data_out = fout.read().replace('\n' , '')
return data_out
The last statement in the function should be the return.
Because there is the print data_out, you return the return value of print which is none.
E.g:
In []: def test():
..: print 'Hello!'
..:
In []: res = test()
Hello!
In []: res is None
Out[]: True

Categories