print next line only if it begins with a specific word - python

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:

Related

Python: Remove first instance only of string from text file

I have been removing the desired lines from a text file by reading it in and rewriting each line if the string(s) I wish to remove are not present, using the following.
with open('infile.txt', 'r') as f:
lines = f.readlines()
with open('outfile.txt', 'w+') as f:
for line in lines:
if line.strip("\n") != "Desired text on line to remove":
f.write(line)
This works fine for all but one of the lines I need to remove which only contains.
1.
This is the first instance of (1.) in the file, and always will be in the files I'm editing; however it is repeated later in the text file and these later instances must be kept - is it possible to remove only the first instance of this text?
If I've understood your question correctly then you want to get rid of first '1.' and keep the rest. It can be done in multiple ways, like below code.
with open('infile.txt', 'r') as f:
lines = f.readlines()
with open('outfile.txt', 'w+') as f:
t = '1.'
for line in lines:
line = line.strip("\n")
if line != "Desired text on line to remove" and line != t:
f.write(line)
f.write("\n")
if line == t:
t = None
One of them is by simply using logical operators (which I've used) and create a variable you want to remove. Which in my case as t. Now use it to filter the first instance. Thereafter change its value to None so that in the next instance it will always be a True statement and the condition to run or not depends on if line is equal to our desired text or not.
P.S.- I've added some more line of codes like line = line.strip("\n") and f.write("\n") just to make the output and code clearer. You can remove it if you want as they don't contribute to clear the hurdle.
Moreover, if you don't get the desired output or my code is wrong. Feel free to point it out as I've not written any answers yet and still learning.

Reading text file - for loop gives unexpected output

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")

Read first line of splitlines() file

I am having trouble converting this code snippet into reading only the first line of the file, rather than opening it random.
Can somebody help?
lines = open('myfile.txt').read().splitlines()
account =random.choice(lines)
If you only want the first line, then only read the first line.
with open('myfile.txt') as f:
line = f.readline()
Above, f.readline() reads until a newline or EOF.
You could take advantage of the fact that the file object itself is iterable:
>>> with open('multiline.txt') as file:
... line1 = next(file)
>>> line1
'this is line 1\n'
This doesn't waste memory by reading the entire file into a list.
However, I'd say #chepner's answer would be the "prescribed" way of doing this.
Starting from your code, you can simply change the second line to
first_line = lines[0]
and you are done.

List the first words per line from a text file in Python

I need to select the first word on each line and make a list from them from a text file:
I would copy the text but it's the formatting is quite screwed up. will try
All the other text is unnecessary.
I have tried
string=[]
for line in f:
String.append(line.split(None, 1)[0]) # add only first word
from another solution, but it keeps returning a "Index out of bounds" error.
I can get the first word from the first line using string=text.partition(' ')[0]
but I do not know how to repeat this for the other lines.
I am still new to python and to the site, I hope my formatting is bearable! (when opened, I encode the text to accept symbols, like so
wikitxt=open('racinesPrefixesSuffixes.txt', 'r', encoding='utf-8')
could this be the issue?)
The reason it's raising an IndexError is because the specific line is empty.
You can do this:
words = []
for line in f:
if line.strip():
words.append(line.split(maxsplit=1)[0])
Here line.strip() is checking if the line consists of only whitespace. If it does only consist of whitespace, it will simply skip the line.
Or, if you like list comprehension:
words = [line.split(maxsplit=1)[0] for line in f if line.strip()]

How to prevent the reading of a text file from stopping at an empty line?

My problem is that my input file contains empty lines (this is a must), but when it reaches an empty line, the: for i, line in enumerate(file): stops reading the file. How do I prevent this.?
The reading of the file is like this, because I need to do something with every one but the last line of a file, than something else with the last line. (This is also a must.)
Here is what I'm trying to do:
with open(sys.argv[1]) as file:
i = 0
for i, line in enumerate(file):
# Do for all but last line
if i < linecount-1:
print "Not last line"
i += 1
# Do for last line
if i == linecount-1:
print "Last line"
It works fine on files without empty lines.
You do not need to declare or increment i in your code. enumerate does that for you. Incrementing additionally as you do probably triggers your conditionals accidentally; it has nothing to do with empty lines.
The mistake in your implementation is explained in the other answer, but to achieve what I think you want to do, it might be better to process as follows, then you don't need to know the length of the file in advance:
import sys
def process_regular_line(line):
print 'regular line', line
def process_last_line(line):
print 'last line:', line
with open(sys.argv[1]) as file:
last_line = file.readline()
while True:
this_line = file.readline()
if not this_line:
process_last_line(last_line)
break
process_regular_line(last_line)
last_line = this_line
For example, on the test file with 5 lines:
a line
another line
a line after a blank line
The last ever line
You get:
regular line: a line
regular line: another line
regular line:
regular line: a line after a blank line
last line: The last ever line

Categories

Resources