I'm trying to print "None" if the input entered by the user is not found in a text file I created. It should also print if the lines if word(s) are found in the text file.
My problem right now is that it is not doing both conditionals. If I were to remove the "line not in user_pass" it would not print anything. I just want the user to be able to know if the strings entered by the user can found in the file and will print that line or "none" if it is not found.
I commented out the ones where I tried fixing my code, but no use.
My code below:
def text_search(text):
try:
filename = "words.txt"
with open(filename) as search:
print('\nWord(s) found in file: ')
for line in search:
line = line.rstrip()
if 4 > len(line):
continue
if line.lower() in text.lower():
print("\n" + line)
# elif line not in text: # the function above will not work if this conditional commented out
# print("None")
# break
# if line not in text: # None will be printed so many times and line.lower in text.lower() conditional will not work
# print("none")
except OSError:
print("ERROR: Cannot open file.")
text_search("information")
I think you need to change for line in search: to for line in search.readlines(): I don't think you're ever reading from the file... Have you tried to just print(line) and ensure your program is reading anything at all?
#EDIT
Here is how I would approach the problem:
def text_search(text):
word_found = False
filename = "words.txt"
try:
with open(filename) as file:
file_by_line = file.readlines() # returns a list
except OSError:
print("ERROR: Cannot open file.")
print(file_by_line) # lets you know you read the data correctly
for line in file_by_line:
line = line.rstrip()
if 4 > len(line):
continue
if line.lower() in text.lower():
word_found = True
print("\n" + line)
if word_found is False:
print("Could not find that word in the file")
text_search("information")
I like this approach because
It is clear where you are reading the file and assigning it to a variable
This variable is then printed, which is useful for debugging
Less stuff is in a try: clause (I like to not hide my errors, but that's not a huge deal here because you did a good job specifying OSError however, what if an OSError occured during line = line.rstrip() for some reason...you would never know!!)
If this helped I'd appreciate if you would click that green check :)
Try this:-
def find_words_in_line(words,line):
for word in words:
if(word in line):
return True;
return False;
def text_search(text,case_insensitive=True):
words = list(map(lambda x:x.strip(),text.split()));
if(case_insensitive):
text = text.lower();
try:
filename = 'words.txt'
with open(filename) as search:
found = False;
for line in search:
line = line.strip();
if(find_words_in_line(words,line)):
print(line);
found = True;
if(not found):
print(None);
except:
print('File not found');
text_search('information');
Didn't really understand your code, so making one on my own according to your requirement.
Related
I am trying to create a block of code that scans a .txt file (first book of LOTR) and tallys the frequency of each letter into a dictionary. (say for instance if theres 500 3 letter words appearing in the book, the output will be displayed as 3:500, and so on).
I seemed to have gotten the code right as I have tried it on test documents before adding the dict() function, and it works by printing each word as a string in a list, though when I go to run it now, it prints the first word then outputs file not found (which is what ive coded for an exception), even though the file is present in my jupyter notebook.
Is there any way to fix this? what is the default directory that jupyter scans for?
All of your help is appreciated!
Code:
fname= input('Enter file: ')
#if len(fname) < 1: fname = 'LOTR.txt'
try:
fhand = open(fname)
d = dict()
for line in fhand:
words = line.split()
print(words)
for word in words:
d1[word] = d1.get(word, 0) + 1
print (d1)
except:
print("File not found")
output:
Enter file: LOTR.txt
['PROLOGUE']
File not found
Your problem is that you are accessing d1 before defining it.
Also you should catch specific exceptions instead of using a bare except. The following should solve your problem:
fname= input('Enter file: ')
#if len(fname) < 1: fname = 'LOTR.txt'
try:
fhand = open(fname)
d = dict()
for line in fhand:
words = line.split()
print(words)
for word in words:
d[word] = d.get(word, 0) + 1
print(d)
except FileNotFoundError:
print("File not found")
except Exception as e:
print("Other error occurred", e)
photo
This code can't load the log.txt file.
The file is in the temp folder.
Why can't I load it?
This code only displays the text: Search word: ABC.
text = input("Search word: ABC")
with open("C:\Temp\log.txt", encoding = "utf-8") as f:
cnt = 0
for line in f:
l = line.strip().split()
if (l[-1] == text):
print(line.strip())
cnt += 1
if (cnt): print(cnt, "count")
else: print(text, "No data.")
It seems like you need to type the word after running the program. The "ABC" you see is the prompt from the script i.e. it is not entered by you. That's why the program keeps running, waiting for the input and doesn't go further.
Here's your code slightly modified to make it clear.
text = input("Search word: ")
with open("C:\Temp\log.txt", encoding="utf-8") as f:
cnt = 0
for line in f:
if text in line:
print(line.strip())
cnt += 1
if cnt:
print(cnt, "count")
else:
print(text, "No data.")
I guess you understand that your code:
ask the user to input some text
count the occurrences of that text in the file C:\Temp\log.txt, under the conditions:
the text does not contains space
the text is at the end of the line
the text may be followed by one or more spaces
the text must be preceded by one or more spaces
the file cannot have empty lines
Under those conditions, your code should behave nicely. I would recommend to change text = input("Search word: ABC") to text = input("Search word: ") to make it clear that the user need to input some text.
If you still have unexpected results, check if you don't have any character encoding issue (like a terminal default encoding not being utf-8)
I am searching a text file for an input word. However, I am only meant to search the text in the file after the word "START". The first twenty-odd before "START" should be ignored. I know how to find "START", but not how to search the rest of the file once "START" is encountered. I would appreciate any guidance!
Here is what I have so far:
file = open("EnglishWords.txt", "r")
print("***** Anagram Finder *****")
word = input("Enter a word: ")
for line in file:
if "START" in line:
if word in line:
print("Yes, ", word, " is in the file.", sep="")
else:
print("Sorry, ", word, " is not in the file.", sep="")
file.close()
Here is a sample of the text file:
The name of Princeton University or Princeton may not be
used in advertising or publicity pertaining to
distribution of the software and/or database. Title to
copyright in this software, database and any associated
documentation shall at all times remain with Princeton
University and LICENSEE agrees to preserve same.
START
clobber
transversalis
squinter
cunner
damson
extrovertive
absorptive
Modifying your code, we have
file = open("EnglishWords.txt", "r")
print("***** Anagram Finder *****")
word = input("Enter a word: ")
start_looking = False
word_found = False
for line in file:
if not start_looking:
if "START" in line:
start_looking = True
else:
continue
if word in line:
print("Yes, ", word, " is in the file.", sep="")
word_found = True
break
if not word_found:
print("Sorry, ", word, " is not in the file.", sep="")
file.close()
As long as START hasn't been found, keep skipping over the lines of the file. If, however, you encounter START, reset your flag and begin looking.
Do a for after your word is found:
with open(myfile, 'r') as f:
for line in f:
if 'START' in line:
# do stuff to lines below 'START'
# you could do another for loop here to iterate
for line in f:
print (line) # just an example
Very similar to this other SO post. Credit for the syntax of my answer comes from its answer.
What about something with regex module ?
re.findall(r"START.*(word_to_search).*", entire_text)
This should return you the result only if there is a START before the word to search for. I hope that's what you're looking for.
EDIT :
For a solution line by line i would go with something like :
start_search = 0
with open(bigfile, "r") as f:
for line in f:
if "START" IN line:
start_search = 1
if start_search and word_to_search in line:
print("result foun")
return (word_to_search)
What about this ?
Keep it short, simple and explicit:
with open("EnglishWords.txt", 'r') as fin:
output = fin.readlines()
# Find the line that contains START
index = output.index("START")
# Search all the lines after that
for line in output[index+1:]:
if word in line:
print("Yes, ", word, " is in the file.", sep="")
else:
print("Sorry, ", word, " is not in the file.", sep="")
You could use Python's dropwhile() to locate the start of the words and iterate from there:
from itertools import dropwhile
print("***** Anagram Finder *****")
word = input("Enter a word: ").lower() + '\n'
with open("EnglishWords.txt") as f_words:
if word in dropwhile(lambda r: not r.startswith("START"), f_words):
print("Yes, {} is in the file".format(word.strip()))
else:
print("Sorry, {} is not in the file.".format(word.strip()))
You can use a boolean :
file = open(“testfile.txt”, “r”)
foundStart = False
for line in file:
if foundStart:
# do something...
elif line == "START":
foundStart = True
Here is what I am trying to do:
I am trying to solve an issue that has to do with wrapping in a text file.
I want to open a txt file, read a line and if the line contains what I want it to contain, check the next line to see if it does not contain what is in the first line. If it does not, add the line to the first line.
import re
stuff = open("my file")
for line in stuff:
if re.search("From ", line):
first = line
print first
if re.search('From ', handle.next()):
continue
else: first = first + handle.next()
else: continue
I have looked a quite a few things and cannot seem to find an answer. Please help!
I would try to do something like this, but this is invalid for triples of "From " and not elegant at all.
lines = open("file", 'r').readlines()
lines2 = open("file2", 'w')
counter_list=[]
last_from = 0
for counter, line in enumerate(lines):
if "From " in line and counter != last_from +1:
last_from = counter
current_count = counter
if current_count+1 == counter:
if "From " in line:
counter_list.append(current_count+1)
for counter, line in enumerate(lines):
if counter in counter_list:
lines2.write(line)
else:
lines2.write(line, '\n')
Than you can check the lines2 if its helped.
You could also revert order of lines, then check in next line not in previous. That would solve your problem in one loop.
Thank you Martjin for helping me reset my mind frame! This is what I came up with:
handle = open("my file")
first = ""
second = ""
sent = ""
for line in handle:
line = line.rstrip()
if len(first) > 0:
if line.startswith("From "):
if len(sent) > 0:
print sent
else: continue
first = line
second = ""
else:
second = second + line
else:
if line.startswith("From "):
first = line
sent = first + second
It is probably crude, but it definitely got the job done!
I'm trying to split a large text files into smaller text files by using a word delimiter. I tried searching but I've only seen posts to break apart files after x lines. I'm fairly new to programming but I've given it a start. I want to go through all the lines, and if it starts with hello, it will put all of those lines into one file until it reaches the next hello. The first word in the file is hello. Ultimately, I'm trying to get the text into R, but I think it would be easier if I split it up like this first. Any help is appreciated, thanks.
text_file = open("myfile.txt","r")
lines = text_file.readlines()
print len(lines)
for line in lines :
print line
if line[0:5] == "hello":
If you are finding for a very simple logic, Try this.
text_file = open("myfile.txt","r")
lines = text_file.readlines()
print len(lines)
target = open ("filename.txt", 'a') ## a will append, w will over-write
hello1Found = False
hello2Found = False
for line in lines :
if hello1Found == True :
if line[0:5] == "hello":
hello2Found = True
hello1Found = False
break ## When second hello is found looping/saving to file is stopped
##(though using break is not a good practice here it suffice your simple requirement
else:
print line #write the line to new file
target.write(line)
if hello1Found == False:
if line[0:5] == "hello": ##find first occurrence of hello
hello1Found = True
print line
target.write(line) ##if hello is found for the first time write the
##line/subsequent lines to new file till the occurrence of second hello
I am new to Python. I just finished a Python for Geographic Information Systems class at Northeastern University. This is what I came up with.
import os
import sys
import arcpy
def files():
n = 0
while True:
n += 1
yield open('/output/dir/%d.txt' % n, 'w')
pattern = 'hello'
fs = files()
outfile = next(fs)
filename = r'C:\output\dir\filename.txt'
with open(filename) as infile:
for line in infile:
if pattern not in line:
outfile.write(line)
else:
items = line.split(pattern)
outfile.write
(items[0])
for item in items:
outfile = next(fs)
outfile.write(item)
filename.close();outfile.close();