So here's the problem I have,
I can find the Search Term in my file but at the moment I can only print out the line that the Search Term is in. (Thanks to Questions posted by people earlier =)). But I cannot print out all the lines to the end of the file after the Search Term. Here is the coding I have so far:-
search_term = r'\b%s\b' % search_term
for line in open(f, 'r'):
if re.match(search_term, line):
print line,
Thanks in advance!
It can be much improved if you first compile the regex:
search_term_regex = re.compile(r'\b%s\b' % search_term)
found = False
for line in open(f):
if not found:
found = bool(search_term_regex.findall(line))
if found:
print line,
Then you're not repeating the print line.
You could set a boolean flag, e.g. "found = True";
and do a check for found==True, and if so print the line.
Code below:
search_term = r'\b%s\b' % search_term
found = False;
for line in open(f, 'r'):
if found==True:
print line,
elif re.match(search_term, line):
found = True;
print line,
To explain this a bit: With the boolean flag you are adding some state to your code to modify its functionality. What you want your code to do is dependent on whether you have found a certain line of text in your file or not, so the best way to represent such a binary state (have I found the line or not found it?) is with a boolean variable like this, and then have the code do different things depending on the value of the variable.
Also, the elif is just a shortening of else if.
Related
I try to learn python by working on projects I really would like to have.
Now I'm at a point where I don't know how to solve it.
I want to search through a file and want lo list all the strings which stand behind an indicator string which also can variate.
Therefore, I need to search for a multiple line string with an unknown number of tabs between the strings and would like to know the string after this (multiple times in a file)
solution.append(
base.fresher(
current = indicator_var,
nominal = unknown_value,
comment = "comment XYZ"
)
)
#comment
solution.append(
base.fresher(
current = indicator_var,
nominal = unknown_value,
comment = "comment ABCDEFG"
)
)
" Base.fresher( current = indicater_var" is something I would like to search. But here I have the problem that I don't know how many tabs are between the "Base.fresher(" and "current = indicater_var". This can varriate.
And how should I proceed after I found it, how do I get the "unknown_value". And this multiple times in one file.
Actually I have no code to show you. I tried it so many times that the result was an unreadable piece of code which is even more confusing.
This is all I have right know:
your_search_word = "base.fresher("
file = open("test_file.txt", "r")
for line in file:
splitted = line.split("\n")
variables.append(splitted)
your_string = variables
list_of_words = your_string.split()
next_word = list_of_words[list_of_words.index(your_search_word) + 1]
print(next_word)
I had a little success with part of this code a few days ago, so I'm clinging to it, but I also know I have no idea how to get anywhere here.
words = []
your_search_word = "base.fresher("
with open("test_file.txt", "r") as f:
found = False
for line in f:
line = line.strip()
if found:
words.append(line)
elif your_search_word == line
found = True
Notes:
with open is maybe safer, because you don't have to close the file and therefore can't forget.
I used strip() to get rid of the newlines
we set a variable to True when a line equals the searchwords
we only add lines to our list 'words' when the found variable is True
With the help of nessuno I made a few changes (remove "=" and ",")
words = []
your_search_word = "current = indicator_var,"
with open("test_file.txt", "r") as f:
found = False
for line in f:
line = line.strip()
if found:
words.append(line.split("= ")[1].split(",")[0])
found = False
elif your_search_word == line:
found = True
print(words)
['unknown_value', 'unknown_value']
This is not the final solution but I have to work on it for a while and post my results here.
Thanks a lot!
I need to check if thre is a call for a function, I know it's not a txt file but when I read a line and try to print using type() it's says str and let me to print all the file correctly but for whatever reason the lines can't be compare and I don't know why, when I compile shows no error, the first line of the file is '#include <Arduino.h>' and the var found is false anyways
import os
def BeforeBuild():
found = False
with open(r"\src\OTAWEBAP.cpp", "r") as f:
for line in f:
print (line)
if(line == '#include <Arduino.h>'):
found = True;
if(not found):
raise Exception("OtaIni function not found, you need to use it to preserve OTA functions in your new deploy")
else:
print('function OtaIni was found')
f.close()
BeforeBuild()
Try replacing
if(line == '#include <Arduino.h>'):
found = True;
with
if line.strip() == '#include <Arduino.h>':
found = True
The strip() function removes ALL whitespace at the beginning and end of the line.
P. S. Try to remember that in Python, if conditions don't need to be in parenthesis and lines don't need semicolons at the end. Otherwise everyone will know that you're really a C programmer at heart.
The last character is \n if you change the line.
So this may work for you:-
import os
def BeforeBuild():
found = False
with open(r"\src\OTAWEBAP.cpp", "r") as f:
for line in f:
print (line)
if(line[:-1] == '#include <Arduino.h>'):
found = True;
if(not found):
raise Exception("OtaIni function not found, you need to use it to preserve OTA functions in your new deploy")
else:
print('function OtaIni was found')
f.close()
BeforeBuild()
Caution should be taken while comparing strings. In this case a whitespace character causing this issue. There are a lot of whitespace characters that could not be seen but may present. So, a good practice while working with this kind of files is to remove these whitespace characters.
You can use strip() to remove whitespace characters from both end of a string.
import os
def BeforeBuild():
found = False
with open(r"\src\OTAWEBAP.cpp", "r") as f:
for line in f:
line = line.strip();
print (line)
if line == '#include <Arduino.h>':
found = True;
if not found:
raise Exception("OtaIni function not found, you need to use it to preserve OTA functions in your new deploy")
else:
print('function OtaIni was found')
f.close()
BeforeBuild()
So I know similar questions have been asked before, but every method I have tried is not working...
Here is the ask: I have a text file (which is a log file) that I am parsing for any occurrence of "app.task2". The following are the 2 scenarios that can occur (As they appear in the text file, independent of my code):
Scenario 1:
Mar 23 10:28:24 dasd[116] <Notice>: app.task2.refresh:556A2D:[
{name: ApplicationPolicy, policyWeight: 50.000, response: {Decision: Can Proceed, Score: 0.45}}
] sumScores:68.785000, denominator:96.410000, FinalDecision: Can Proceed FinalScore: 0.713463}
Scenario 2:
Mar 23 10:35:56 dasd[116] <Notice>: 'app.task2.refresh:C6C2FE' CurrentScore: 0.636967, ThresholdScore: 0.410015 DecisionToRun:1
The problem I am facing is that my current code below, I am not getting the entire log entry for the first case, and it is only pulling the first line in the log, not the remainder of the log entry, and it appears to be stopping at the new line escape character, which is occurring after ":[".
My Code:
all = []
with open(path_to_log) as f:
for line in f:
if "app.task2" in line:
all.append(line)
print all
How can I get the entire log entry for the first case? I tried stripping escape characters with no luck. From here I should be able to parse the list of results returned for what I truly need, but this will help! ty!
OF NOTE: I need to be able to locate these types of log entries (which will then give us either scenario 1 or scenario 2) by the string "app.task2". So this needs to be incorporated, like in my example...
Before adding the line to all, check if it ends with [. If it does, keep reading and merge the lines until you get to ].
import re
all = []
with open(path_to_log) as f:
for line in f:
if "app.task2" in line:
if re.search(r'\[\s*$', line): # start of multiline log message
for line2 in f:
line += line2
if re.search(r'^\s*\]', line2): # end of multiline log message
break
all.append(line)
print(all)
You are iterating over each each line individually which is why you only get the first line in scenario 1.
Either you can add a counter like this:
all = []
count = -1
with open(path_to_log) as f:
for line in f:
if count > 0:
all.append(line)
if count == 1:
tmp = all[-count:]
del all[-count:]
all.append("\n".join(tmp))
count -= 1
continue
if "app.task2" in line:
all.append(line)
if line.endswith('[\n'):
count = 3
print all
In this case i think Barmar solution would work just as good.
Or you can (preferably) when storing the log file have some distinct delimiter between each log entry and just split the log file by this delimiter.
I like #Barmar's solution with nested loops on the same file object, and may use that technique in the future. But prior to seeing I would have done it with a single loop, which may or may not be more readable:
all = []
keep = False
for line in open(path_to_log,"rt"):
if "app.task2" in line:
all.append(line)
keep = line.rstrip().endswith("[")
elif keep:
all.append(line)
keep = not line.lstrip().startswith("]")
print (all)
or, you can print it nicer with:
print(*all,sep='\n')
I run my own business from home and started use Python 2 days ago. I'm trying to write a script that will search through my log files line by line and tell me if a system doesn't match my mandatory naming scheme. There are multiple different schemes and I want the script to look for them all. I've tried using a list (as seen below) but that won't work and then I tried with normal brackets and that gave me an error (requires left operand, not tuple). I've noted the lines that give me a problem.
#variables
tag = ["DATA-", "MARK", "MOM", "WORK-"] #THIS ONE!!!!!!
#User Input
print "Please select Day of the week"
print "1. Monday"
print "2. Tuesday"
print "3. Wednesday"
print "4. Thursday"
print "5. Friday"
print "6. Saturday"
print "7. Sunday"
day = input("> ")
#open appropriate file and check to see if 'tag' is present in each line
#then, if it doesn't, print the line out.
if day == 1:
f = open('F:\DhcpSrvLog-Mon.log', 'r')
for line in f:
if tag in line: #THIS ONE!!!!!!!!!!!!!
pass
else:
print line
Any tips or tricks would be most appreciated!
I suggest rewriting the code like this:
with open('F:\DhcpSrvLog-Mon.log', 'rU') as f:
for line in f:
for t in tag:
if t in line: break
else:
print line
Using with you automagically close the file on exit of the block, so you needn't worry about forgetting to close it. Using else: in the for loop only triggers if you don't break out of the loop earlier.
if day == 1:
f = open('F:\DhcpSrvLog-Mon.log', 'r')
for line in f:
if tag in line: #THIS ONE!!!!!!!!!!!!!
pass
else:
print line
replace with
if day == 1:
f = open('F:\DhcpSrvLog-Mon.log', 'r')
for line in f:
if [x for x in tag if x in line]: #THIS ONE!!!!!!!!!!!!!
pass
else:
print line
use any to check this. It is more efficient because it will not try all the tags if it find that one is in line.
any(x in line for x in tag)
contains_tag=False
for t in tag:
if t in line:
contains_tag=True
break # Found a match, break out of for loop
if not contains_tag:
print (line)
First you need to loop through each of the tags (e.g. for t in tag). Then you need to check if the string t is contained in line.
Since you're only looking for one tag that does match, the simplest way is to keep track with a boolean variable.
If you wanted to only look for log messages that start with that tag, you could say if line.startswith(t) instead of if t in line
hi there got a couple of probs, say in my text file i have:
abase
abased
abasement
abasements
abases
This coding below is meant to find a word in a file and print all the lines to the end of the file. But it doesnt it only prints out my search term and not the rest of the file.
search_term = r'\b%s\b' % search_term
for line in open(f, 'r'):
if re.match(search_term, line):
if search_term in line:
f = 1
if f: print line,
Say i searched for abasement, i would like the output to be:
abasement
abasements
abases
My final problem is, i would like to search a file a print the lines my search term is in and a number of lines befer and after the searchterm. If i searched the text example above with 'abasement' and i defined the number of lines to print either side as 1 my output would be:
abased
abasement
abasements
numb = ' the number of lines to print either side of the search line '
search_term = 'what i search'
f=open("file")
d={}
for n,line in enumerate(f):
d[n%numb]=line.rstrip()
if search_term in line:
for i in range(n+1,n+1+numb):
print d[i%numb]
for i in range(1,numb):
print f.next().rstrip()
For the first part of the question, unindent your if f: print line,. Otherwise, you're only trying to print when the regex matches.
It's not clear to me what your question is in the second part. I see what you're trying to do, and your code, but you've not indicated how it misbehaves.
For the first part the algorithm goes like this (in pseudo code):
found = False
for every line in the file:
if line contains search term:
found = True
if found:
print line