1

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?

wovano
  • 4,543
  • 5
  • 22
  • 49
vai_007
  • 29
  • 5
  • Does this answer your question? [How do I split the definition of a long string over multiple lines?](https://stackoverflow.com/questions/10660435/how-do-i-split-the-definition-of-a-long-string-over-multiple-lines) – sahasrara62 Jul 14 '22 at 10:00
  • 1
    @sahasrara62, that's a completely different question. It's about the *definition* of a long string, so about splitting something in the source code. This question is about splitting input text according to a certain pattern, which is really different. – wovano Jul 14 '22 at 17:19

4 Answers4

3

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']
Hugh Mungus
  • 382
  • 1
  • 7
2

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']
ko3
  • 1,757
  • 5
  • 13
1
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']
Colim
  • 1,283
  • 3
  • 11
0

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']
sahasrara62
  • 10,069
  • 3
  • 29
  • 44