Split string by number of words with python

Question:

How do I split up a string into several parts of a number of words in python. For example, turn a 10,000 word string into ten 1,000 word strings. Thanks.

Asked By: usertest

||

Answers:

Under normal circumstances :

>>> a = "dedff fefef fefwff efef"
>>> a.split()
['dedff', 'fefef', 'fefwff', 'efef']
>>> k = a.split()
>>> [" ".join(k[0:2]), " ".join(k[2:4])]
['dedff fefef', 'fefwff efef']
>>> 
Answered By: pyfunc
def splitter(n, s):
    pieces = s.split()
    return (" ".join(pieces[i:i+n]) for i in range(0, len(pieces), n))

for piece in splitter(1000, really_long_string):
    print(piece)

Where n is number of words; s is the long string.
This will yield ten 1000 word strings from a 10000 word string like you ask.
Note that you can also use iterools grouper recipe but that would involve making 1000 copies of the iterator for your string: expensive I think.

Also note that this will replace all whitespace with spaces. If this isn’t acceptable, you’ll need to try something else.

Answered By: aaronasterling

Pehaps something like this,

>>> s = "aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo pp qq rr ss tt uu vv"
>>> chunks = s.split()
>>> per_line = 5
>>> for i in range(0, len(chunks), per_line):
...     print " ".join(chunks[i:i + per_line])
... 
aa bb cc dd ee
ff gg hh ii jj
kk ll mm nn oo
pp qq rr ss tt
uu vv
Answered By: plundra

this might help:

s="blah blah .................."
l =[]
for i in xrange(0,len(s),1000):
    l.append(s[i:i+1000])
Answered By: jknair

Try this:

s = 'a b c d e f g h i j k l'
n = 3

def group_words(s, n):
    words = s.split()
    for i in xrange(0, len(words), n):
        yield ' '.join(words[i:i+n])

list(group_words(s,n))
['a b c', 'd e f', 'g h i', 'j k l']
Answered By: eumiro

If you’re comfortable using regular expressions, you could also try this:

import re

def split_by_number_of_words(input, number_of_words):
    regexp = re.compile(r'((?:w+W+){0,%d}w+)' % (number_of_words - 1))
    return regexp.findall(input)

s = ' '.join(str(n) for n in range(1, 101)) # "1 2 3 ... 100"
for words in split_by_number_of_words(s, 10):
    print words
Answered By: Steef
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.