1

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####
2
  • Hi, can you please clarify why you've accepted an answer that does not answer your question? You're trying to count lines that begin with a pound sign, not words. Or else, you'd not get 4 like you expect. Please take another look at the answers. Commented Apr 21, 2018 at 7:26
  • Just want to count who start with "#", i'm not familiar with "startwith". Thank you for your response. Commented Apr 23, 2018 at 5:03

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

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)

1 Comment

Besides that, there's a lot wrong with this answer. Please don't encourage opening files without explicitly closing them or using context managers. Also, please don't put parentheses where necessary.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.