0

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 :)

Denis Wasilew
  • 503
  • 3
  • 9
  • 29

4 Answers4

1

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)
Ruben Bermudez
  • 2,293
  • 14
  • 22
0

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.

Nitish
  • 6,358
  • 1
  • 15
  • 15
0

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))
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
0

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
jfs
  • 399,953
  • 195
  • 994
  • 1,670