How to split a line by an apostrophe 'and -?
'
-
For example, given string = "pete - he a boy"
string = "pete - he a boy"
You can use the decomposition function of the regular expression module:
re.split("['-]", "pete - he a boy")
string = "pete - he a boy" result = string.replace("'", "-").split("-") print result ['pete ', ' he', a boy']
, :
string.replace("-", "'").split("'")
. split ( ) - , @Cédric Julien fooobar.com/questions/1073506/...)
,
l = [x.split("'") for x in "pete - he a boy".split('-')]
print ( [item for m in l for item in m ] )
['pete ', ' he', a boy']
>>> import re >>> string = "pete - he a boy" >>> re.split('[\'\-]', string) ['pete ', ' he', a boy']
, :)
import re string = "pete - he a boy" print re.findall("[^'-]+",string)
.
import re string = "pete - he a boy" print re.findall("[^'-]+",string) print re.findall("(?! )[^'-]+(?<! )",string)
['pete', 'he', a boy']