Python Forum
split string and keep separator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
split string and keep separator
#1
often discussed on the interwebs, but no i idiot-proof solution available.

i want to split the following list of strings by the "=" and keep the left part incl. the "=", and the re module should NOT be used for this.

>>> print(options)
['SYSTEM=LDAP', 'registry=LDAP']
output should look like this -> ['SYSTEM=', 'registry=']

best would be to integrate this in this generator

def arguments_generator(options):
        for element in options:
                yield '-a'
                yield element
Reply
#2
You can use result = [s[:1+s.find('=')] or s for s in options]
Reply
#3
wow, thank you, works perfectly!

>>> mylist = ['SYSTEM=LDAP', 'registry=LDAP']
>>> result = [s[:1+s.find('=')] or s for s in mylist]
>>> print(result)
['SYSTEM=', 'registry=']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  coma separator is printed on a new line for some reason tester_V 4 418 Feb-02-2024, 06:06 PM
Last Post: tester_V
  doing string split with 2 or more split characters Skaperen 22 2,317 Aug-13-2023, 01:57 AM
Last Post: Skaperen
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,054 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  [split] Parse Nested JSON String in Python mmm07 4 1,413 Mar-28-2023, 06:07 PM
Last Post: snippsat
  Printing a raw string with a folder separator at the end, duplicates the separator krulah 5 1,164 Nov-28-2022, 12:41 PM
Last Post: snippsat
  Split string using variable found in a list japo85 2 1,238 Jul-11-2022, 08:52 AM
Last Post: japo85
  Split string knob 2 1,840 Nov-19-2021, 10:27 AM
Last Post: ghoul
  Split string between two different delimiters, with exceptions DreamingInsanity 2 1,978 Aug-24-2020, 08:23 AM
Last Post: DreamingInsanity
  split string enigma619 1 2,029 May-20-2020, 02:47 PM
Last Post: perfringo
  Split string with multiple delimiters and keep the string in "groups" DreamingInsanity 4 6,352 May-12-2020, 09:31 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020