Seite 1 von 1

Re: Anzahl von Wörtern in String zählen

Verfasst: Freitag 17. Mai 2013, 11:52
von snafu
Hier mal eine Variante, die häppchenweise lesen kann:

Code: Alles auswählen

def count_words(stream, chunk_size=1024*1024):
    num_words = 0
    saw_nonspace = False
    chunk_reader = iter(lambda: stream.read(chunk_size), '')
    for chunk in chunk_reader:
        num_chunk_words = len(chunk.split())
        if saw_nonspace and not chunk[0].isspace():
            num_chunk_words -= 1
        num_words += num_chunk_words
        saw_nonspace = not chunk[-1].isspace()
    return num_words

def test():
    from StringIO import StringIO
    s = StringIO(" as  s   s  s  ")
    print count_words(s, 2)


if __name__ == '__main__':
    test()