简体   繁体   中英

Split string by number of words with python

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.

this might work

def splitter(n, s):
    pieces = s.split()
    return (" ".join(pieces[i:i+n]) for i in xrange(0, len(pieces), n)

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

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 something else.

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']
>>> 

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']

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

this might help:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM