Home > Net >  Count a string in a text file with Python
Count a string in a text file with Python

Time:09-25

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

CodePudding user response:

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)

CodePudding user response:

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)

CodePudding user response:

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()]
  • Related