0

So I am trying to count the number of times a certain character occurs from a score (.txt) file. The file says something like [$$$$$#$$#$$$]

Im trying to create a counter that counts the number of times $ occurs but resets every time a # happens.

This is all I have come up with so far but doesn't account for the restart.

with open ("{}".format(score), 'r') as f:
    scoreLines = f.read().splitlines()
    y = str(scoreLines)
    $_count = int(y.count('$'))

The count is reflected in another part of the program which is outputting a wave. So every time a # occurs the wave needs to stop and start again. Any help would be appreciated!

2
  • 1
    Do you want to keep feeding the count of $ to the wave outputting module every time you see a #? Commented Apr 16, 2018 at 6:03
  • @RamkishoreM yes
    – AJ4314
    Commented Apr 16, 2018 at 6:04

2 Answers 2

2

You can simply use split

for sequence in y.split('#'):
    wave_outputting_function(.., len(sequence), ...)

(assuming y contains your entire file as a string without linebreaks)

0

Try this using iterators:

#!/usr/bin/env python3

import sys


def handle_count(count):
    print("count", count)


def main():
    with open("splitter.txt", "rt") as fhandle:
        for line in (d.strip() for d in fhandle):
            for d in (i.count('$') for i in line.split('#')):
                handle_count(d)


if __name__ == '__main__':
    main()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.