Reputation: 1111
I m trying to open up a text file and look for string Num_row_lables
. If the value for Num_row_labels
is greater than or equal to 10, then print the name of the file.
In the example below, my text file test.mrk
has some text in the format below: P.s., my text file doesn't have Num_row_labels >= 10
. It always has "equal to
".
Format= { Window_Type="Tabular", Tabular= { Num_row_labels=10 } }
so I created a variable teststring
to hold the pattern I will be looking at.
Then I opened the file.
Then using re
, I got Num_row_labels=10
in my variable called match.
Using group()
on match, I extracted the threshold number I wanted and using int()
converted the string to int
.
My purpose is to read the text file to find/print
the value for Num_row_labels along with the name of file if the text file has Num_row_labels = 10 or any # greater than 10.
Here's my test code:
import os
import os.path
import re
teststring = """Format= { Window_Type="Tabular", Tabular= { Num_row_labels=10 } }"""
fname = "E:\MyUsers\ssbc\test.mrk"
fo = open(fname, "r")
match = re.search('Num_row_labels=(\d+)', teststring)
tnum = int(match.group(1))
if(tnum>=10):
print(fname)
How do I make sure that I m searching the match in the content of opened file and checking the condition for tnum>=10? My test code would simply print the file name only on the basis of last 4 lines. I want to be sure that the search is all over the content of my text file.
Upvotes: 1
Views: 7133
Reputation: 338
Python code to read file content based on condition
file = '../input/testtxt/kaggle.txt'
output = []
with open(file, 'r') as fp:
lines = fp.readlines()
for i in lines:
if('Image for' in i):
output.append(i)
print(output)
Upvotes: 3
Reputation: 3259
so what you want to do is to read out the whole file as a string, and search for your pattern on that string
with open(fname, "r") as fo:
content_as_string = fo.read()
match = re.search('Num_row_labels=(\d+)', content_as_string)
# do want you want to the matchings
Upvotes: 4