3

I am a beginner in Python. I am trying to count a specific character in a text file (a string). I manage to read in the file and with a for loop I try to loop through each row in the text file and find a specific character. The goal is to print out the count of that character. Unfortunately I get 0 as a result although I have a couple of that character in the file.

in_file= open("file_x.txt")
lines = in_file.readlines()

d=0

for line in in_file:
    if d in line:
        d=d+1
print(d)

Any suggestion? Thank you

5
  • 1
    if d in line: here d is your int variable you have declared above. use 'd'. Commented Sep 24, 2021 at 17:27
  • 1
    Also, your loop should loop over the lines, not over in_file. Commented Sep 24, 2021 at 17:27
  • 1
    for line in in_file: d+=line.count('d') Commented Sep 24, 2021 at 17:28
  • If a character that you are counting can appear more than once on the same row, you way of counting will miss it. Commented Sep 24, 2021 at 17:29
  • or for line in in_file: for i in line: if i=='d': d+=1 Commented Sep 24, 2021 at 17:30

3 Answers 3

4

You have a couple problems.

First, when reading a file you should specify the mode (not strictly needed, but greeatly helps clarify intent). In this case for reading do:

open("file_x.txt", "r")

Next, when reading a file you need to make sure you close it after you're done. You should use with for this:

with open("file_x.txt", "r") as in_file:
    lines = in_file.readlines()

You didn't loop the lines, you looped the already read file in_file. Change to:

for line in lines:

You didn't use a string to check the line, you used the variable d which is an int 0. Change to "d"

if "d" in line:

All together now:

with open("file_x.txt", "r") as in_file:
    lines = in_file.readlines()

d = 0

for line in lines:
    if "d" in line:
        d += 1

print(d)

Another error. If you want to count all occurrences and not just how many lines contain the letter, you'll want to use str.count. Also you can avoid calling readline if you directly loop over the file:

d = 0
with open("file_x.txt", "r") as in_file:
    for line in in_file:
        d += line.count("d")

print(d)

Going a bit further with sum and a generator expression:

with open("file_x.txt", "r") as in_file:
    d = sum(line.count("d") for line in in_file)

print(d)
Sign up to request clarification or add additional context in comments.

3 Comments

Is it really an error to use the default mode? If so, why does the default even exist?
@don'ttalkjustcode I guess it's not an error, but I can't tell you how many bugs I've seen because people forget that one. To each their own.
Thank you. Can I use the generator expressions if I have more characters to search? If I have a,b,c,d for example and I want to count all of them
1

You need to use a separate variable to count the number of occurrences and loop over lines instead of in_file.
I've added another loop to count the exact number of occurrences within each line as if 'd' in line will not include the total count if the number of occurrences in a line exceeds 1.

in_file= open("file_x.txt")
lines = in_file.readlines()

count = 0
key = 'd'

for line in lines:
    for j in line:
        if j == key:
            count+=1

print(count)

Comments

0

If you care about the efficency I would suggest generate each line inside a generator function.

def gen_lines():
    with open("file_x.txt", "r") as in_file:
        for line in in_file:
            yield line 

And than you could calculate it using list comperhension in just one line.

elements = sum[line.count("d") for line in gen_lines()]

2 Comments

I find opening files inside a generator risky as the consumer of the generator object is not guaranteed to reach the StopIteration exception which would close your file handle. Consider the example any(gen_lines()). This will stop at the first line and not close the file. You should open the file outside the method and pass it to gen_lines. Although in_file by itself is already iterable by lines, so there's no need.
Also if you did want to do this, more concisely you can use yield from and write yield from in_file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.