简体   繁体   中英

Split string by Nth occurrence of a character

I have a string with some comma-separated values:

my_string = 'abc,kjj,hg,kj,ls,jsh,ku,lo,sasad,hh,da'

I need to split this string by every 5th occurrence of a comma.

Code I tried:

a = re.findall("\,".join(["[^,]+"] * 5), my_string)

Current output:

['abc,kjj,hg,kj,ls', 'jsh,ku,lo,sasad,hh']

Expected output:

['abc,kjj,hg,kj,ls', 'jsh,ku,lo,sasad,hh', 'da']

How to get remaining string?

You can do it by splitting on , and joining in chunks:

seq = my_string.split(',')
size = 5
[','.join(seq[pos:pos + size]) for pos in range(0, len(seq), size)]

Output:

['abc,kjj,hg,kj,ls', 'jsh,ku,lo,sasad,hh', 'da']

You do not need regex for this. Just try something like this:

ls = 'abc,kjj,hg,kj,ls,jsh,ku,lo,sasad,hh,da'.split(",")
[",".join(ls[i:i+5]) for i in range(0, len(ls), 5)]

results into this:

['abc,kjj,hg,kj,ls', 'jsh,ku,lo,sasad,hh', 'da']
str_test = "abc,kjj,hg,kj,ls,jsh,ku,lo,sasad,hh,da"

splitStr = str_test.split(",")
_5str = [",".join(splitStr[i : i + 5]) for i in range(0, len(splitStr), 5)]
print(_5str)
>>>['abc,kjj,hg,kj,ls', 'jsh,ku,lo,sasad,hh', 'da']

you can do it this way by counting the , occuerence

>>> my_string = 'abc,kjj,hg,kj,ls,jsh,ku,lo,sasad,hh,da'
>>> start = 0
>>> end = 0
>>> count = 0
>>> 
>>> result = []
>>> for i, v in enumerate(my_string):
...     if v==',':
...             count+=1
...     if count ==5:
...             count = 0
...             result.append(my_string[start:i])
...             end = i
...             start = i+1
... 
>>> if end != len(my_string)-1:
...     result.append(my_string[star:])

>>> if end != len(my_string)-1:
...     result.append(my_string[start:])
... 
>>> result
['abc,kjj,hg,kj,ls', 'jsh,ku,lo,sasad,hh', 'da']

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