I am trying to figure out how to search for a string in a text file, and if that string is found, output the next line.
I've looked at some similar questions on here but couldn't get anything from them to help me.
This is the program I have made. I have made it solely to solve this specific problem and so it's also probably not perfect in many other ways.
def searcher():
print("Please enter the term you would like the definition for")
find = input()
with open ('glossaryterms.txt', 'r') as file:
for line in file:
if find in line:
print(line)
So the text file will be made up of the term and then the definition below it.
For example:
Python
A programming language I am using
If the user searches for the term Python, the program should output the definition.
I have tried different combinations of print (line+1) etc. but no luck so far.
your code is handling each line as a term, in the code below f is an iterator so you can use next to move it to the next element:
with open('test.txt') as f:
for line in f:
nextLine = next(f)
if 'A' == line.strip():
print nextLine
If your filesize is small, then you may simply read the file by using readlines() which returns a list of strings each delimited by \n character, and then find the index of the selected word, and the print the item at position + 1 in the given list.
This can be done as:
def searcher():
print("Please enter the term you would like the definition for")
find = input()
with open("glossaryterms.txt", "r") as f:
words = list(map(str.strip, f.readlines()))
try:
print(words[words.index(find) + 1])
except:
print("Sorry the word is not found.")
You could try it Quick and dirty with a flag.
with open ('glossaryterms.txt', 'r') as file:
for line in file:
if found:
print (line)
found = False
if find in line:
found = True
It's just important to have the "if found:" before setting the flag. So if you found your search term next iteration/line will be printed.
In my mind, the easiest way would be to cache the last line. This means that on any iteration you would have the previous line, and you'd check on that - keeping the loop relatively similar
For example:
def searcher():
last_line = ""
print("Please enter the term you would like the definition for")
find = input()
with open ('glossaryterms.txt', 'r') as file:
for line in file:
if find in last_line:
print(line)
last_line = line
Related
To start, I am a complete newb to coding and don’t know what I’m doing.
I am working with a database txt file and have got it imported and open. I need to now loop through the file, find a specific keyword (number), and print this out to a new file. I have tried endlessly to understand coding to no avail. Can someone explain how to do this to me. Please explain in a dumbed down way so an idiot like me can understand.
file1 = open('database.txt', 'r')
Lines = file1.readlines()
pattern = "gene_numbers_here"
for line in Lines:
if pattern in line:
print(..., file = open("gene1found.txt",'w'))```
Use readlines to load up the txt file line by line into a list of strings
file1 = open('myfile.txt', 'r')
Lines = file1.readlines()
Now for the looping:
for line in Lines:
print(line)
Based on your problem, you are actually wanting to do a "pattern search" in a string.
For that, just use the same code from the looping example and insert a "pattern search" function to check if your pattern exists in your txt file, line by line.
# declare the pattern
pattern = "this_pattern_only"
# loop through the list of strings in Lines
for line in Lines:
# patter search statement
if pattern in line:
print("pattern exist")
else:
print("pattern does not exist")
If you want to print this to a file, just change the print code lines I made.
Check out more on the write functionalities here:
https://www.w3schools.com/python/python_file_write.asp
Based on you new info about the code, try this:
# file1 is database, file2 is output
file1 = open('database.txt', 'r')
file2 = open('gene1found.txt', 'w')
Lines = file1.readlines()
pattern = "gene_numbers_here"
# search and write lines with gene pattern
print("Searching database ...")
for line in Lines:
if pattern in line:
file2.write(line)
print("Search complete !")
# close the file
file1.close()
file2.close()
This will write the gene lines with the pattern you want to your file.
I am learning how to read txt files and find something in them. The example below outputs the entire txt file. I am trying to get it to print out "found it" when it finds the word "thanks" in the txt file. Where am I wrong?
This is the txt file I am reading:
this is a
demo file
for exercises
thanks
bye
This is the code I have written:
f = open("demo.txt", "r")
print(f.readline())
print(f.readline())
for word in f:
print(word)
if word == "thanks":
print("found it")
This is the output:
this is a
demo file
for exercises
thanks
bye
Process finished with exit code 0
with open("demo.txt", "r") as f:
for word in f:
print(word)
if "thanks" in word:
print("found it")
break
Files are iterable, so if you want to read a text file line by line, all you have to do is iterate over it. Also, you must ensure the file is closed after use - which is easily done using the with statement. And, finally, lines ends with the (system-dependant) newline marker, which you may want to strip for comparisons.
IOW, your code should look something like:
# nb: "r" (read) is the default
with open("path/to/your/file") as f:
for line in f:
# removes the ending newline marker
line = line.rstrip("\n")
print(line)
# given your spec 'when it finds the word "thanks"'
# I assume that it doesn't matter if there's
# something else in the line, so we test for
# containment.
if "thanks" in line:
print("found it")
I am a bit new to python and I was wondering if anyone can help. Basically I am reading contents of a file and when I find the word "prb" I want to check the next line using the next() function and if it starts with the word "rt", i want to print both lines. So far I wrote this piece of code:
with open('/home/user/Desktop/3rdstep.txt', 'r') as f:
f.readline()
for line in f:
if "prb" in line:
try:
myword = next(f)
if "rt" in myword:
print(line.strip())
print(myword)
except:
print("pass")
This works fine but the only problem is that it skips randomly "rt" words for a reason I don't know. Can anyone help please or have someone done something similar?
Thanks
If your input has two consecutive lines starting with 'prb' followed by line starting with 'rt' then they are skipped. The only exception is the case where they are the first three lines in the file. This is because for line in f: reads the first line starting with 'prb' and myword = next(f) reads the second line. Thus on the following iteration line starts with 'rt'.
Instead of reading the next line you could store the previous line and then check if two lines match:
prev = ''
with open('/home/user/Desktop/3rdstep.txt') as f:
for line in f:
if prev.startswith('prb') and line.startswith('rt'):
print(prev.strip())
print(line)
prev = line
You may use if myword.startswith("rt"): instead of if "rt" in myword:
Hello I'm trying to grab data from a keyword in a text document as a project, I am able to do this using this code. I am very new to python and im not sure where to start to troubleshoot this issue.
data_file = open("test.txt", "r")
Keyword = raw_input("Please enter the keyword: ")
go = False
start = Keyword
end = "[+][+]"
with open("test.txt") as infile:
for line in infile:
line = line.strip()
if start in line: go = True
elif end in line:
go = False
continue
if go:
print(line)
This code works great for a sample text document like
Something Something Something Something
Something Something Something Something
Something Keyword:
Data
Data
Data
Data
End
Something
However i run into an issue when trying to read from a file that has strange characters. for example:
2015/08/14 15:48:30 OUT:
2015/08/14 15:48:30 OUT:
PQ=
(3< ’’aÈ©ÿY˜ü â [+][+]52
2015/08/14 15:48:31:IN[+]53[+][+]101[+]-1[+] **Keyword** ,SHOWALL
**data**
**data**
**data**
**data**
**data**
**data**
**data**
end
Since the goal is to read from this text document and just print out the words in between the Keyword and End. it will not execute if it has these characters in them. and for the project I can not remove these characters it just has to be able to read through the document and find the keyword and print out whats in between.
Any ideas on how i can read from a text document that has these strange characters with it processing it correctly rather than just crashing.
First you need to open the file in binary mode. You could then use a regular expression to extract all the text between your entered keyword and "end". Whole words could then be extracted using another regular expression:
import re
with open("input.txt", "rb") as f_input:
start_token = raw_input("Please enter the start keyword: ")
end_token = raw_input("Please enter the end keyword: ")
reText = re.search("%s(.*?)%s" % (re.escape(start_token), re.escape(end_token)), f_input.read(), re.S)
if reText:
for word in re.findall(r"\b\w+\b", reText.group(1)):
print word
else:
print "not found"
For your example text this would display:
SHOWALL
data
data
data
data
data
data
data
Or if you just want all of the text between the two points, print reText.group(1) instead of the for loop.
Updated: added support for a variable end token.
The file contains binary content so it should be opened in binary mode
You can do this by doing
data_file = open("test.txt", "rb")
I'm new to programming pretty much in general and I am having difficulty trying to get this command to print it's output to the .txt document. My goal in the end is to be able to change the term "Sequence" out for a variable where I can integrate it into a custom easygui for multiple inputs and returns, but that's a story for later down the road. For the sake of testing and completion of the current project I will be just manually altering the term.
I've been successful in being able to get another program to send it's output to a .txt but this one is being difficult. I don't know if I have been over looking something simple, but I have been grounded for more time than I would like to have been on this.
When the it searches for the lines it prints the fields in the file I want, however when it goes to write it finds the last line of the file and then puts that in the .txt as the output. I know the issue but I haven't been able to wrap my head around how to fix it, mainly due to my lack of knowledge of the language I think.
I am using Sublime Text 2 on Windows
def main():
import os
filelist = list()
filed = open('out.txt', 'w')
searchfile = open("asdf.csv")
for lines in searchfile:
if "Sequence" in lines:
print lines
filelist.append(lines)
TheString = " ".join(filelist)
searchfile.close()
filed.write(TheString)
filed.close()
main()
It sounds like you want to the lines you are printing out collected in the variable "filelist", which will then be printed to the file at the .write() call. Only a difference of indentation (which is significant in Python) prevents this from happening:
def main():
import os
filelist = list()
filed = open('out.txt', 'w')
searchfile = open("asdf.csv")
for lines in searchfile:
if "Sequence" in lines:
print lines
filelist.append(lines)
TheString = " ".join(filelist)
searchfile.close()
filed.write(TheString)
filed.close()
main()
Having
filelist.append(lines)
at the same level of indentation as
print lines
tells Python that they are in the same block, and that the second statement also belongs to the "then" clause of the if statement.
Your problem is that you are not appending inside the loop, as a consequence you are only appending the last line, do like this:
for lines in searchfile:
if "Sequence" in lines:
print lines
filelist.append(lines)
BONUS: This is the "pythonic" way to do what you want:
def main():
with open('asdf.csv', 'r') as src, open('out.txt', 'w') as dest:
dest.writelines(line for line in src if 'sequence' in line)
def main():
seq = "Sequence"
record = file("out.txt", "w")
search = file("in.csv", "r")
output = list()
for line in search:
if seq in line: output.append(line)
search.close()
record.write(" ".join(output))
record.close()