I am trying to find word count by find a match line if match is found go to next line and count the word in that line
id = open('id.txt','r')
ids = id.readlines()
for i in range(0, len(ids) - 1, 1):
actual_id = ids[i]
print(actual_id)
with open('sample.txt', 'r') as f:
for line in f:
if re.search(r'\{actual_id}\|RQ', line):
next_line = line.next()
solution_count = line.count('Solution')
print("The solution count is " str(solution_count))
Sample.txt text file:
[07-12-2022 13:27:45.728|Info|0189B31C|RQ]
<ServiceRQ><SaleInfo><CityCode Solution=1>BLQ</CityCode><CountryCode Solution=2>NL</CountryCode><CurrencyCode>EUR</CurrencyCode><Channel>ICI</Channel></ServiceRQ>
[07-12-2022 13:27:45.744|Info|0189B31D|RQ]
<ServiceRQ><SaleInfo><CityCode Solution=1>BLQ</CityCode><CountryCode>NL</CountryCode><CurrencyCode>EUR</CurrencyCode><Channel>ICI</Channel></ServiceRQ>
0189B31C
0189B31D
These are unique id's which are store in different text file I am trying to read the 1st id from text file and match that id in Sample.txt and if match is found go to next line and count the number of Solution words and print. Please also help me with regex I want to match {Id from text file}|RQ to find the match in line. Please can someone help me for find the code I am little confused.
CodePudding user response:
- You need to use an f-string to substitute
actual_id
into the regexp. - Put
|
before{actual_id}
so it won't match a partial string. - Use
f.readline()
to read the next line, notline.next()
- Count the words in
next_line
, notline
.
id = open('id.txt','r')
ids = id.readlines()
for actual_id in ids[:-1]
print(actual_id)
with open('sample.txt', 'r') as f:
for line in f:
if re.search(rf'\|{actual_id}\|RQ', line):
next_line = f.readline()
solution_count = next_line.count('Solution')
print(f"The solution count is {solution_count}")
CodePudding user response:
re.search()
doesn't return a boolean value. It returns are.Match
object, if a match is found, andNone
if not. So your check should beif re.search(...) is not None
- Your
actual_id
variable isn't formatted into your regex string. Userf"..."
as suggested by @Barmar. - If your 'sample.txt' is the same file for each iteration of the outer look, you probably just want to read it once and store it as list of strings, since reading a file is slow and reading the same file again and again has no point.
- The inner loop is more likely a case for a while loop, since a loop that eats sometimes two and times one line of an iterator/file can be confusing, and
readline()
won't help you, if you use my hint of readingsample.txt
only once. The body of your outer loop could be
it = iter(samples)
try:
line = next(it)
while True:
if ...:
solution_count = next(it).count("solution")
...
line = next(it)
except StopIteration:
pass