Reputation: 27628
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.
Upvotes: 2
Views: 8381
Reputation: 71014
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.
Upvotes: 6
Reputation: 34665
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
Upvotes: 0
Reputation: 212885
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']
Upvotes: 2
Reputation: 4764
this might help:
s="blah blah .................."
l =[]
for i in xrange(0,len(s),1000):
l.append(s[i:i+1000])
Upvotes: 0
Reputation: 19232
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
Upvotes: 0
Reputation: 66709
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']
>>>
Upvotes: 3