Dance Party
Dance Party

Reputation: 3713

How to split a string into chunks per number of characters and delimiter?

Given:

s = 'foo, bar, baz, qudz'

I would like to split this string given 2 criteria:

  1. a character count n (in this case, I'll use n = 10)
  2. the delimiter: ", " (before the nth character)

Desired Result:

['foo, bar', 'baz, qudz']

I know I can split it by delimiter easily enough:

s.split(', ')
['foo', 'bar', 'baz', 'qudz']

I also know that I can split it into even chunks of n like this:

[s[i:i+n] for i in range(0, len(s), n)]

I've also seen where I can split by the nth delimiter here.

Upvotes: 1

Views: 485

Answers (2)

Todd
Todd

Reputation: 5405

You can use a regular expression to get the substrings like so...

>>> re.findall(r"[\w,][\w, ]{1,8},?", "foo, bar, baz, qudz")
['foo, bar,', 'baz, qudz']

I find myself making edits to this after I posted. Regular expressions are tricky, but this is close to a solution if not exact. Maybe some fine tuning needed. There might be a space at the end of matches - that could be trimmed off.

Upvotes: 1

You could use functools.reduce to accomplish this.

import functools


def splitter(s, n):
    def helper(acc, v):
        tmp1 = acc[-1] 
        tmp2 = len(tmp1)
        if tmp2 >= n or tmp2 + len(v) >= n:
            acc.append(v)
        else:
            acc[-1] = tmp1 + ',' + v

        return acc

    tmp1 = s.split(',')
    if len(tmp1) == 1:
        return tmp1

    return list(functools.reduce(helper, tmp1[1:], [tmp1[0]]))

Upvotes: 2

Related Questions