DaShield
DaShield

Reputation: 51

Count string with the first character match

Looking for a way to count the string who have "#" as first character.

import re

def y():
    with open('test.conf', 'r') as rf:
    hitcount = 0
        for line in rf:
            if re.search(r'#*', line):
            hit_count = hit_count + 1
    print(hit_count)

when I used the script...it count all the string who have #, wherever it was place.

Below are the test.conf. The result should be only 4.

#config-version=FWF60C-5.02-FW-build754-170421:opmode=0:vdom=0:user=lorenzo.aspera
#conf_file_ver=42514
#buildno=0754
#global_vdom=1
config sy###stem global
   END####

Upvotes: 1

Views: 90

Answers (2)

alkaliexce
alkaliexce

Reputation: 35

Your approach and your description of the problem seems slightly different.

To clarify, which of the bottom 2 options is what you want?

  1. find any lines with # before it
  2. find any words with # before it?

if its (1), I believe that coldspeed's answer will serve the purpose.

if its (2), then the steps are as follows:

Load file and delimit whitespaces and newlines:

words = open(file).read().split()

Check through each word for #

count=0
for (i in words): 
    if(i.startswith("#")):
        count+=1
print(count)

Upvotes: 0

cs95
cs95

Reputation: 402603

Yeah, you shouldn't be using re.search... actually, you shouldn't be using re AT ALL. Why not just str.startswith?

count = 0
with open('test.conf', 'r') as rf:
    for line in rf:
        if line.startswith('#'):
            count += 1

print(count)
4

If you're hell bent on using regex, then, use re.match instead, that anchors searches to the start of the string (re.search does not - unless you use the SOL anchor ^ inside your pattern - that's why you were observing spurious counts).

Alternatively, I like conciseness (but not at the cost of readability of course), so I'd go for sum with a comprehension.

with open('test.conf', 'r') as rf:
    count = sum(line.startswith('#') for line in rf) 

print(count)
4

Upvotes: 2

Related Questions