I want to recover this four lines tagged ERROR, contained in a file:
ERROR Blablabalbalablabalbalablabalbalablabalbalablabalbalablabalbala
ERROR Tototototototototototototototototototototototototototototototot
ERROR Hihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihi
hihihihihihihihihihihihihihihihihi
ERROR Lalalalalalalalala
def getErrorWarningInfo(self, file, line, tag):
msg = line.strip(tag)
while True:
nextline = file.readline()
if (' ' in nextline):
msg += "\n"+nextline.strip()
break
return [self.id, tag, msg] ,line
I recover only 3 ERROR including the one containing two lines, but I can't get the line before this one:
ERROR Tototototototototototototototototototototototototototototototot
And when I remove, in the function, the line file.readline(), I recover the 4 ERROR but only the first line of the one with two lines.
Why don't You use file.read().split('ERROR') and then remove whitespaces? (:
Here is a prototype to get around your issue. You can adjust it for your need if you like it.
I've edited a log file to contain INFO and WARNING too
ERROR Blablabalbalablabalbalablabalbalablabalbalablabalbalablabalbala
ERROR Tototototototototototototototototototototototototototototototot
INFO tik tok
ERROR Hihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihi
hihihihihihihihihihihihihihihihihi
WARNING bip bup
ERROR Lalalalalalalalala
# types of tags you can encounter in log
TAGS = ["ERROR", "INFO", "WARNING"]
LOG = "./LOG"
with open(LOG) as log:
lines = []
# you want to keep previous line in case next line is continuation of it
prev_line = ""
for line in log.readlines():
# Check if line contains any tags
if any(map(line.__contains__, TAGS)):
if prev_line:
# If we already have a previous line, we can add it to the list.
# The current line also contains a tag
lines.append(prev_line)
# Current line becomes previous
prev_line = line
continue
# If there is no tag in the line, it most be continuation of previous.
prev_line += line
# Check that all lines concatenated correctly
for line in lines:
print line
print("============ Filtering logs =============\n")
# Now you can filter your lines by log tags
def get_errors(lines):
return [line for line in lines if "ERROR" in line]
# Only ERROR logs
for line in get_errors(lines):
print line
Outputs
ERROR Blablabalbalablabalbalablabalbalablabalbalablabalbalablabalbala
ERROR Tototototototototototototototototototototototototototototototot
INFO tik tok
ERROR Hihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihi
hihihihihihihihihihihihihihihihihi
WARNING bip bup
============ Filtering logs =============
ERROR Blablabalbalablabalbalablabalbalablabalbalablabalbalablabalbala
ERROR Tototototototototototototototototototototototototototototototot
ERROR Hihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihi
hihihihihihihihihihihihihihihihihi
def parse_file(self):
for file in self.prt_files:
with open(os.path.join(self.path, file), "r") as f:
try:
for line in f:
if (line.startswith("ERROR")):
error, line = self.getErrorWarningInfo(f, line, "ERROR")
[...]
Related
I've been trying to add a save function to a game by having the user enter their initials which gets saved to a file with their score so then it can load were they were before.
At the start it asks for their initials and saves it to a file, then I want to the file to be copied onto a list so the score can be edited, however the list doesn't include the most recent initials and score added the file.
I don't know where the problem is so I've added the file stuff I did.
Names.write('~~~')
Names.write('\n')
Names.write(username_score)
Names.write('\n')
line = Names.readlines()
print(line)
with open('Names.txt','r+') as Names:
for line_number, data in enumerate(Names, start=1):
if username in data:
print(f"Word '{username}' found on line {line_number}")
break
print('data',data)
print('line',line)
line.pop(1)
line.insert(1, username_score)
print('line 2',line)
for i in range(len(line)):
Names.write(line[i])
I think this is because in your first code block, you are trying to write and then read the file without closing and reopening the file. The line = Names.readlines() line should be in the second code block like so:
username_score = '200'
username = 'foo'
with open('Names.txt','w+') as Names:
Names.write('~~~')
Names.write('\n')
Names.write(username_score)
Names.write('\n')
line = Names.readlines()
print(line)
with open('Names.txt','r+') as Names:
line = Names.readlines()
print(line)
for line_number, data in enumerate(line, start=0):
if username in data:
print(f"Word '{username}' found on line {line_number}")
break
print('data',data)
print('line',line)
line.pop(1)
line.insert(1, username_score)
print('line 2',line)
for i in range(len(line)):
Names.write(line[i])
This full example should be easy enough to integrate into your code, and if you have any questions leave a comment.
I've been working on a function for hours and I just can't see the solution since this is my first time working with opening .txt files, etc.
My function will open a .txt file of 50 names, with the first line (a header) being NAMES. And then it will create a list of those names. I need to test the function so if the line 'NAMES' is not in the txt file it will raise an exception. I have been working on the 'NAMES' part for longer than I care to admit and can't see what I am doing wrong. Here is my code:
EOF = ''
def load_names(fName):
global line, names
print(end = f"Opening file {fName} ...")
try:
f = open(fName, 'r')
except:
raise Exception(f"OOPS! File {fName} not found")
print(end = "reading...")
line = f.readline().strip()
while line.strip() != 'NAMES':
line = f.readline().strip()
while line != EOF and line.strip() != 'NAMES':
raise Exception("!! Oops! Missing line 'NAMES' !!" )
names = [] # To collect names from file
line = f.readline().strip() # Read in first name
while line != EOF:
if line =='\n':
print("!! Oops! Blank line not allowed !!")
names.append(line.strip())
line = f.readline()
f.close()
print(end = "closed.\n")
return names
The 'blank line not allowed' works when tested, but the way I have this code written now, even If I open a file that does have the 'NAMES' line in it, it still gives the error "Exception("!! Oops! Missing line 'NAMES' !!" )". I'm not sure how to do it, basically.
The files i am testing this with look like:
With NAMES -
NAMES
Mike
James
Anna
Without NAMES -
Mike
James
Anna
Your function seems much too complex, and the exception handling is wrong. For example, it will replace a "permission denied" exception with a generic Exception with an incorrect message saying the file wasn't found, even though that was not the reason for the error.
Instead, probably
avoid the use of global variables;
don't trap errors you can't meaningfully handle;
don't strip() more than once - save the stripped value so you can use it again;
simply read one line at a time, and check that it passes all your criteria.
def load_names(fName):
with open(fName, 'r') as f:
seen_names = False
names = []
for line in f:
line = line.strip()
if line == 'NAMES':
seen_names = True
elif line == '':
raise Exception("!! Oops! Blank line not allowed !!")
else:
names.append(line)
if not seen_names:
raise Exception("!! Oops! Missing line 'NAMES' !!" )
return names
If you actually want NAMES to be the first line in the file, it's not hard to change the code to require that instead. Maybe then it does make sense to read the first line separately before the loop.
def load_names(fName):
with open(fName, 'r') as f:
seen_names = False
names = []
for line in f:
line = line.strip()
if not seen_names:
if line == 'NAMES':
seen_names = True
else:
raise Exception("!! Oops! Missing line 'NAMES' !!" )
elif line == '':
raise Exception("!! Oops! Blank line not allowed !!")
else:
names.append(line)
return names
Concretely, the bug in your original attempt is that the code continues to read lines until it gets one which doesn't contain NAMES, and then complains.
Can someone help me identify what im doing wrong below:
def SeeIfExactRangeIsFound():
with open(logfile) as input_data:
mylist = []
for line in input_data:
if BeginSearchDVar in line: # Or whatever test is needed
print line.strip()
#mylist.append((line.strip()))
#return mylist
break
for line in input_data: # This keeps reading the file
if line.strip() == EndinSearchD:
break
print line
#mylist.append((line))
#return mylist
#SeeIfExactRangeIsFound()
LRange = SeeIfExactRangeIsFound()
print LRange
I'm looping through a file and only printing out sections of that file. As in, I start printing content of logfile when a specific pattern is found in the line being read. and continue printing all lines after that first line until a line containing the pattern found in EndingSearchD variable is found.
this works with the "print". but as you can see, I want to store the output of the SeeIfExactRangeIsFound function in a variable and use the content of that variable later on.
My problem is, despite my attempts (commented out below) to try different ways to accomplish my goal, none of it seems to work. I feel I'm so close to the answer but I spent 2 hours on this and can't figure it out.
any ideas?
a version matching your description instead of your code
I start printing content of logfile when a specific pattern is found
in the line being read. and continue printing all lines after that
first line until a line containing the pattern found in EndingSearchD
variable is found.
def SeeIfExactRangeIsFound():
with open(logfile) as input_file:
input_data = input_file.readlines()
mylist = []
allow_yielding = False
for line in input_data:
if BeginSearchDVar in line:
allow_yielding = True
if allow_yielding:
yield line
if line.strip() == EndinSearchD:
break
LRange = SeeIfExactRangeIsFound()
print LRange
You almost got it, but your return statement is not in the proper scope:
def SeeIfExactRangeIsFound():
with open(logfile) as input_data:
mylist = []
for line in input_data:
if BeginSearchDVar in line: # Or whatever test is needed
print line.strip()
mylist.append((line.strip()))
break
for line in input_data: # This keeps reading the file
if line.strip() == EndinSearchD:
break
print line
mylist.append((line))
return mylist
as bonus, you can easily transform this into a generator:
def SeeIfExactRangeIsFound():
with open(logfile) as input_data:
for line in input_data:
if BeginSearchDVar in line: # Or whatever test is needed
yield line.strip()
for line in input_data: # This keeps reading the file
if line.strip() == EndinSearchD:
break
yield line
and consume it like:
results = [x for x in def SeeIfExactRangeIsFound()]
I'm making a program that stores data in a text file, I can search for data line by line, and I made a (delete function) that is quoted below, making a variable 'a' adding to it the (non deleted lines), and ask before deletion for results and if not confirmed it would be added also to 'a', then rewrite the (file) with'a' omitting the deleted lines.
THE PROBLEM IS:
all results are deleted not only the confirmed one desbite that:
#deleting line
confirm = input('confirm to delete [y]/[n]>>')
if confirm != 'y':
a += line
so, why did this problem happen and how to fix it?
Next is the whole code of delete function:
searching = input('enter any information about query: ')
searching = searching.lower() # converting words in lower case
f = open(file, 'r')
lines = f.readlines()
f.close()
print('Word | Definition | Remarks')
a = '' # we will store our new edited text here
for line in lines:
line_lower_case = line.lower() # changing line in lower case temporary
# because contact != COntact and will not appear in searcch
if searching in line_lower_case:
print('Query found')
print()
print('>>',line, end = '') # printing words in the same case as been added
# end = '', to prevent printing new line avoiding extra empty line
#deleting line
confirm = input('confirm to delete [y]/[n]>>')
if confirm != 'y':
a += line
#elif confirm =='y':
# pass # it will just do nothing, and will not add line to 'a'
continue # to search for more queries with the same searching entry
print()
a += line #we add each line to the 'a' variable
f = open(file,'w')
f.write(a) #we save our new edited text to the file
f.close()
I changed the indentations of the program and that was the issue as I agreed with #TheLazyScripter and that should work now if I understood your problem correctly, I did a bunch of tests and they did work. I noticed that you didn't define what input file will be and I add that line of code at line 3 which will through an error if the file not defined.
searching = input('enter any information about query: ')
searching = searching.lower() # converting words in lower case
file = "test.txt" #your file
f = open(file, 'r')
lines = f.readlines()
f.close()
print('Word | Definition | Remarks')
a = '' # we will store our new edited text here
for line in lines:
line_lower_case = line.lower() # changing line in lower case temporary
# because contact != COntact and will not appear in searcch
if searching in line_lower_case:
print('Query found')
print()
print('>>',line, end = '') # printing words in the same case as been added
# end = '', to prevent printing new line avoiding extra empty line
#deleting line
confirm = input('confirm to delete [y]/[n]>>')
if confirm != 'y':
a += line
#elif confirm =='y':
# pass # it will just do nothing, and will not add line to 'a'
continue # to search for more queries with the same searching entry
print()
a += line #we add each line to the 'a' variable
f = open(file,'w')
f.write(a) #we save our new edited text to the file
f.close()
I've got a pretty simple python script that reads in a file, and parses it line by line.
It doesn't seem to recognize the '//' at the start of my lines. If I change it to look for '#' at the start of my lines, it doesn't find those lines either. Am I just misunderstanding this?
line = fIn.readline()
while line:
print "line is", line
line = line.strip()
if line.startswith('//'):
print "winner"
line = fIn.readline()
The file I'm reading in looks like this:
// Feedback
"Feedback" = "Feedback";
// New strings
"File URL not reachable." = "File URL not reachable.";
And the debug line looks appropriate when it prints out:
line is // Feedback
line is "Feedback" = "Feedback";
line is
line is // New strings
line is "File URL not reachable." = "File URL not reachable.";
line is
Better version:
with open("abc") as f:
for line in f:
line=line.strip()
if line and line.startswith("//"):
print "line is",line
print "winner"
print next(f)
....:
output:
line is // Feedback
winner
"Feedback" = "Feedback";
line is // New strings
winner
"File URL not reachable." = "File URL not reachable.";
You are only reading one line of your text file. Other than you have the wrong indent on the last line, it seems to work. Try running your program after making sure line = fIn.readline() gets executed on each iteration (move it one block to the left).
Here is what I get after fixing that one line, is this the desired output?
line is // Feedback
winner
line is "Feedback" = "Feedback";
line is
line is // New strings
winner
line is "File URL not reachable." = "File URL not reachable.";
Edit: does this work for you?
for line in open("yourfile.txt").readlines():
print "line is", line
line = line.strip()
if line.startswith('//'):
print "winner"
try this
for line in fIn:
print "line is", line
line = line.strip()
if line[0:2]=='//':
print "winner"
line = fIn.readline()