How to search for multiple string in s single gzip file? - python-3.x

I am trying to open a zip file and search for a specific string. Here, if 'result =one ' then I should search for another specific string called 'next' in the same file but in different lines and print line that contains 'next'.
example:
next line
result = one
asdfgh
...
waiting for next line
please wait to print the next line
So first it should search for 'result = one' and then it should search for 'next' in a file.
Expected output :
next line
waiting for next line
please wait to print the next line
work I am trying
with gzip.open ('result.gz', 'rt') as i:
for line in i:
if 'result = one' in line:
continue
if 'next' in line:
print (line)
When I am searching for 'result = one' I can find it and when I am trying to search for the 'next' string it is not giving me any output. It is giving the exact line that I need only when I am doing it alone. Any help in solving this would be appreciated. Thank you

Think of it as searching for an arbitrary string in some order. You start with 'result = one'. If that's found, you keep doing the same thing, but for 'next'.
You can actually make the algorithm work for a general sequence, where the last element is printed when it's found.
keys = ['result = one', 'next']
search = enumerate(keys, start=1)
i, key = next(search)
for line in file:
if key in line:
if i == len(keys):
print(line)
break
else:
i, key = next(search)
else:
print('Not found')

The problem with your code is you use continue in your for loop. continue basically ends the current iteration, so it won't execute your if 'next' in line:.
After your updated text, it is still not very clear, but this code might do what you want.
#! /usr/bin/env python3
import gzip
with gzip.open('result.gz', 'rt') as i:
find_next = False
for line in i:
if not find_next and 'result = one' in line:
find_next = True
elif find_next and 'next' in line:
print(line.strip())
I use a flag called find_next. It starts with a False. It is set to True after it finds result = one. Basically, it changes the state to find the word next until the end of the file. You can adjust the code as you wanted.
Edited the solution:
OP wrote in the description that 'next' can be found in the same file, but from the comment on my answer, 'next' is expected in the same line:
I would use a regex here which is easier:
#! /usr/bin/env python3
import gzip
import re
# Find 'result = one' then any characters followed by 'next' till the end of the line.
PATTERN = re.compile("(result = one).+(next).*")
with gzip.open('result.gz', 'rt') as i:
for line in i:
if PATTERN.search(line):
print(line.strip())

Related

to print the next line using pythons loop

i matched a line using regex and now i wanna to print the line next to all the matched line.
import re
file = open(input("Input-file name : ") , "r")
fi = file.readlines()
f=file.readline()
pat=r'^[^\n(E|P):]:\s[EXINTF_DATA\d]\d'
for line in fi:
if re.match(r'^[^\n(E|P):]:\s[EXINTF_DATA\d]\d',line):
print(line.strip())#all the 'Startpoint:'s from the file is getting printed
s=line.strip()
for s in range(len(fi)-1):
if 'input port clocked by CLK' in fi:
print(s.strip())
i also tried last for loop part like this,
for s in range(len(fi)-1):
l=line.startswith('input port clocked by CLK')
print(l)#but this loop was running continoulsy
am trying to print till the string found after printing the next line. am also attaching my text file and how it looks like
am a very beginner in python. can someone help me on this, please.

How do I find multiple strings in a text file?

I need all the strings found in the text file to be found and capitalized. I have found out how to find the string but getting multiple is my issue if you can help me print, where the given string is throughout my code, would be great thanks.
import os
import subprocess
i = 1
string1 = 'biscuit eater'
# opens the text file
# if this is the path where my file resides, f will become an absolute path to it
f = os.path.expanduser("/users/acarroll55277/documents/Notes/new_myfile.txt")
# with this form of open, the wile will automatically close when exiting the code block
txtfile = open (f, 'r')
# print(f.read()) to print the text document in terminal
# this sets variables flag and index to 0
flag = 0
index = 0
# looks through the file line by line
for line in txtfile:
index += 1
#checking if the sting is in the line or not
if string1 in line:
flag = 1
break
# checking condition for sting found or not
if flag == 0:
print('string ' + string1 + ' not found')
else:
print('string ' + string1 + ' found in line ' + str(index))
I believe your approach would work, but it is very verbose and not very Pythonic. Try this out:
import os, subprocess
string1 = 'biscuit eater'
with open(os.path.expanduser("/users/acarroll55277/documents/Notes/new_myfile.txt"), 'r+') as fptr:
matches = list()
[matches.append(i) for i, line in enumerate(fptr.readlines()) if string1 in line.strip()]
fptr.read().replace(string1, string1.title())
if len(matches) == 0: print(f"string {string1} not found")
[print(f"string {string1} found in line {i}") for i in matches]
This will now print out a message for every occurrence of your string in the file. In addition, the file is handled safely and closed automatically at the end of the script thanks to the with statement.
You can use the str.replace-method. So in the line where you find the string, write line.replace(string1, string1.upper(), 1). The last 1 is there to only make the function replace 1 occurence of the string.
Either that or you read the text file as a string and use the replace-method on that entire string. That saves you the trouble of trying to find the occurence manually. In that case, you can write
txtfile = open(f, 'r')
content = txtfile.read()
content = content.replace(string1, string1.upper())

argparse read in txt file

I am trying to read a text file. In the second step I loop through the files and then I am trying to show the results in the command line.
I have a couple of problems:
I am not sure whether I managed to read in the text file, there is no error message, but
the outcome does not come
I get an error "UnboundLocalError: local variable 'P' referenced before assignment - although
I defined the variables before the function
The function works and prints the desired value but not when running it in the command line
with argparse
The code runs as "python filename.py textfile" in the command line
Checking other threads on agrparse did not help.
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('filename')
args = parser.parse_args()
A = 0 # the number of characters are 0 in the beginning
B = 0
C = 0
D = 0
with open(args.filename) as file:
def rooms():
with open("rooms.txt", "r") as in_file:
lines = in_file.readlines()
for line in lines:
if "A" in line:
W+=line.count("A") #I use the count method to count each character in a specific row
if "B" in line:
B+=line.count("B")
if "C" in line:
C+=line.count("C")
if "D" in line:
D+=line.count("D")
rooms()
# if __name__=='main__main':
print(f'total:\nA: {A} B: {B} C: {C} D: {D}')
Any help would be appreciated
There are many issues with the code if it is full code:
1)indentation : Not clear indentation in your code, so it is not executed as intended.
2)syntax: There should be at least 2 blank lines after completing your function code, so that code goes into function.
3)Separate out your function definition & function call. Create your function definition (def rooms()) outside file open.
You have some issues with you code, some of which is mentioned by KiranM's answer.
Other issues are:
You have two different sources for accepting files, argsparse and a hard coded filename.
You can accomplish want you want with a dictionary and if statement.
You read in the lines twice, with the for loop and in_file.readlines().
def room(filename::str): # or a Path object.
letters = {"A":0, "B":0, "C":0, "D":0}
with open(filename, "r") as in_file:
for line in in_file:
if line in letters:
letters[line] = letters[line] + 1
return letters
# In main:
print(room("room.txt"))

Python: Reading line with 'readline()' function and appending to a list

My code:
In my file i have these numbers in a list
charge_account = ['4654145', '9658115', '5658845', '5658045', '6181531', '2134874', '5964554']
I am reading the file with a function, appending it to a list and then returning the list:
import os
os.system('cls')
def fileReader():
contentList = []
with open('charge_accounts.txt','r') as f:
line = f.readline().rstrip('\n')
while line !="":
line = f.readline().rstrip(' \n')
contentList.append(line)
# print(contentList)
# print(len(contentList))
#contentList = contentList[:-1]
print(contentList)
return contentList
Now my question is, when i read all the file content and append them to my list, i am getting an extra blank string at the end of the list.
output:
['4654145', '9658115', '5658845', '5658045', '6181531', '2134874', '5964554', '']
Now i have solved it by using slicing (as i commented them out) but i still have not figured out why i am getting the ' ' in the end of the list. i tried filtering it out but noting happens. i have checked if it there is an extra line in the end of the file but what am i doing wrong ?
There are a couple of things. You are reading the file line by line in the while loop. This means that after the last line is read, the while condition is still true so you read an extra line (which is empty) but still added to your list.
But you don't need a while loop: use lines = f.readlines(). It will read the whole file in a list, and you almost have the list you are aiming for. Almost, because you need to strip each element:
def fileReader():
with open('charge_accounts.txt','r') as f:
lines = f.readlines()
return [line.strip() for line in lines]
print(fileReader())
while line !="":
contentList.append(line)
line = f.readline().rstrip(' \n')
print(contentList)
I realized i had to append the while loop primer into the list which i read before the loop started. content.append(line) had to be the first statement in the while loop. This solves the blank entry in the end of list, which in hindsight i realize means that i skipped the first readline value.

Enumerating, and printing lines in Python.

Okay, I am building a little program that will help single out Nmap results:
#Python3.7.x
#
#
#
#report=input('Name of the file of Nmap Scan:\n')
#target_ip=input('Which target is the report needed on?:\n')
report = "ScanTest.txt"
target_ip = "10.10.100.1"
begins = "Nmap scan report for"
fhand = open(report,'r')
beginsend = "Network Distance:"
for num1,line in enumerate(fhand, 1):
line = line.rstrip()
if line.startswith(begins) and line.endswith(target_ip):
print(num1)
for num2,line in enumerate(fhand, 1):
line = line.rstrip()
if line.startswith(beginsend):
print(num2)
In my what im trying to do is get the first part of the scan results "target_ip" and with that i hope i can read the lines from there until there is a break in the line of the txt.
What this code does for me now is just get me the line number where i want to start.
In the second part of the code I tried getting the number of line for the last bit of text that i need. But it wont print. Im not sure if im going about this the right way or im not looking hard enough.
In short find my line and print until there is a break in the text.
The first loop exhausts all the lines in the file. When the second loop tries to run, there are no more lines to read and the loop exits immediately.
If you want the first loop to stop when it finds a matching line and allow the second loop to read the remaining lines, you can add a break statement in the if.
start_pattern = 'hello there'
end_pattern = 'goodblye now'
print_flag = False
with open('somefile.txt') as file:
for line in file:
if start_pattern in line:
print_flag = True
if print_flag:
print line
if end_pattern in line:
break

Resources