undefined
undefined

Reputation: 513

Counting chars in a file | python 3x

I'm wondering, how can I count for example all "s" characters and print their number in a text file that I'm importing? Tried few times to do it by my own but I'm still doing something wrong. If someone could give me some tips I would really appreciate that :)

Upvotes: 0

Views: 114

Answers (4)

jfs
jfs

Reputation: 414149

Here's a version with a reasonable time performance (~500MB/s on my machine) for ascii letters:

#!/usr/bin/env python3
import sys
from functools import partial

byte = sys.argv[1].encode('ascii') # s
print(sum(chunk.count(byte)
          for chunk in iter(partial(sys.stdin.buffer.read, 1<<14), b'')))

Example:

$ echo baobab | ./count-byte b
3

It could be easily changed to support arbitrary Unicode codepoints:

#!/usr/bin/env python3
import sys
from functools import partial

char = sys.argv[1]
print(sum(chunk.count(char)
          for chunk in iter(partial(sys.stdin.read, 1<<14), '')))

Example:

$ echo ⛄⛇⛄⛇⛄ | ./count-char ⛄
3

To use it with a file, you could use a redirect:

$ ./count-char < input_file

Upvotes: 0

Ruben Bermudez
Ruben Bermudez

Reputation: 2323

Open the file, the "r" means it is opened as readonly mode.

filetoread = open("./filename.txt", "r")

With this loop, you iterate over all the lines in the file and counts the number of times the character chartosearch appears. Finally, the value is printed.

total = 0
chartosearch = 's'
for line in filetoread:
    total += line.count(chartosearch)
print("Number of " + chartosearch + ": " + total)

Upvotes: 1

A.J. Uppal
A.J. Uppal

Reputation: 19264

You open the file with an open("myscript.txt", "r") with the mode as "r" because you are reading. To remove whitespaces and \n's, we do a .read().split(). Then, using a for loop, we loop over each individual character and check if it is an 'S' or an 's', and each time we find one, we add one to the scount variable (scount is supposed to mean S-count).

filetoread = open("foo.txt").read().split()
scount = 0
for k in ''.join(filetoread):
    if k.lower() == 's':
        scount+=1

print ("There are %d 's' characters" %(scount))

Upvotes: 0

Nitish
Nitish

Reputation: 7116

I am assuming you want to read a file, find the number of s s and then, store the result at the end of the file.

f = open('blah.txt','r+a')
data_to_read = f.read().strip()
total_s = sum(map(lambda x: x=='s', data_to_read ))
f.write(str(total_s))
f.close()

I did it functionally just to give you another perspective.

Upvotes: 0

Related Questions