Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting strings in Python using specific characters

Tags:

python

split

I'm trying to split an inputted document at specific characters. I need to split them at [ and ] but I'm having a difficult time figuring this out.

def main():
for x in docread:
    words = x.split('[]')
    for word in words:
        doclist.append(word)

this is the part of the code that splits them into my list. However, it is returning each line of the document.

For example, I want to convert

['I need to [go out] to lunch', 'and eat [some food].']

to

['I need to', 'go out', 'to lunch and eat', 'some food', '.']

Thanks!

like image 609
user1044868 Avatar asked Dec 27 '22 10:12

user1044868


1 Answers

You could try using re.split() instead:

>>> import re
>>> re.split(r"[\[\]]", "I need to [go out] to lunch")
['I need to ', 'go out', ' to lunch']

The odd-looking regular expression [\[\]] is a character class that means split on either [ or ]. The internal \[ and \] must be backslash-escaped because they use the same characters as the [ and ] to surround the character class.

like image 82
Greg Hewgill Avatar answered Jan 12 '23 17:01

Greg Hewgill