Anzahl von Wörtern in String zählen

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
Benutzeravatar
snafu
User
Beiträge: 6740
Registriert: Donnerstag 21. Februar 2008, 17:31
Wohnort: Gelsenkirchen

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()
Antworten