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.
Related
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())
sample input
in parsing a text file .txt = ["'blah.txt'", "'blah1.txt'", "'blah2.txt'" ]
the expected output in another text file out_path.txt
blah.txt
blah1.txt
blah2.txt
Code that I tried, this just appends "[]" to the input file. While I also tried perl one liner replacing double and single quotes.
read_out_fh = open('out_path.txt',"r")
for line in read_out_fh:
for word in line.split():
curr_line = re.findall(r'"(\[^"]*)"', '\n')
print(curr_line)
this happens because while you reading a file it will be taken as string and not as a list even if u kept the formatting of a list. thats why you getting [] while doing re.for line in read_in_fh: here you are taking each letters in the string thats why you are not getting the desired output. so iwrote something first to transform the string into a list. while doing that i also eliminated "" and '' as you mensioned. then wrote it in to a new file example.txt.
Note: change the file name according to your files
read_out_fh = open('file.txt',"r")
for line in read_out_fh:
line=line.strip("[]").replace('"','').replace("'",'').split(", ")
with open("example.txt", "w") as output:
for word in line:
#print(word)
output.write(word+'\n')
example.txt(outputfile)
blah.txt
blah1.txt
blah2.txt
The code below works out for your example you gave in the question:
# Content of textfile.txt:
asdasdasd=["'blah.txt'", "'blah1.txt'", "'blah2.txt'"]asdasdasd
# Code:
import re
read_in_fh = open('textfile.txt',"r")
write_out_fh = open('out_path.txt', "w")
for line in read_in_fh:
find_list = re.findall(r'\[(".*?"*)\]', line)
for element in find_list[0].split(","):
element_formatted = element.replace('"','').replace("'","").strip()
write_out_fh.write(element_formatted + "\n")
write_out_fh.close()
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
Here is a snippet from a text file I am working on.
http://pastebin.com/4Uba5i4P
I would like to use python to detect those big repeating "~ Move" lines (Which are not identical except for the "~ Move" part.), and remove all but the first and last of those lines.
How I would I start to go about this?
You could read the file line by line like this:
`## Open the file with read only permit
f = open('myTextFile.txt')
## Read the first line
line = f.readline()
## If the file is not empty keep reading line one at a time #
# till the file is empty while line: print line
line = f.readline() f.close()`
With this you could then edit this sample to test each line using a regex like this:
`if line.find("~Move") == -1:
Break;
Else:
Line=Line [5:-1]`
Though this assumes that the ~Move is all at the beginning of the line. Hope this helps, if not leave a comment and I'll try and help.
I'm using Pycharm and have been very happy so far. However, today I ran into a issue that I can't figure out or explain. The code will prompt the user for an input file. The file is a .txt file that contains lines of words. After the user provides the filename, the program will open it, remove white spaces at the end of the lines and print the contents of the file. (lots_of_words.txt = example)
INPUT
print(lots_of_words.txt)
OUTPUT
Programming is fun and will save the world from errors! ....
Here is the part of the code that is causing the confusion:
user_input = input('Enter the file name: ')
open_file = open(user_input)
for line in open_file:
line = line.rstrip()
read_file = open_file.read()
print(read_file)
OUTPUT
Process finished with exit code 0
Now by just removing the for loop with string.rstrip(), the text file prints fine:
INPUT
user_input = input('Enter the file name: ')
open_file = open(user_input)
# Removed for loop
read_file = open_file.read()
print(read_file)
OUTPUT
Programming is fun and will save the world from errors! ....
I'm using python 3.4 with Pycharm IDE. I realize that the script completed fine without errors, but why won't it print the final variable? I'm sure this is a simple answer, but I can't figure it out.
Running the same code in Python 2.7, prints fine even with string.rstrip().
It has nothing to do with PyCharm.
Your for moves the pointer to the end of the file. To use open_file again, use seek(0), before printing.
open_file = open(user_input)
for line in open_file:
line = line.rstrip()
open_file.seek(0)
read_file = open_file.read()
print(read_file)
Not the most efficient solution though (if efficiency matters in given situation), since you read all the lines twice. You can either store each line after reading it (as suggested in the other answer), or print each line after striping it.
Also, rstrip() will remove whitespaces at the end of the string, but not '\n'.
Irrelevant: You should use with open() as.. : instead of open() since it closes the file automatically.
Iterating over your file object in the for loop will consume it, so there will be nothing left to read, you're simply discarding all lines.
If you want to strip all whitespace from all lines, you could use:
user_input = input('Enter the file name: ')
open_file = open(user_input)
lines = []
for line in open_file:
lines.append(line.rstrip())
print(''.join(lines))
or even shorter:
print(''.join(line.rstrip() for line in open_file))