split()Python中的多个指定分隔符

split()Python中的多个指定分隔符,python,python-3.x,Python,Python 3.x,如何在python中的split()中指定多个分隔符 input = "print{hello world};x = 2 + 3;" x = input.split('{') #this works but not the desired OP x = input.split('{','}') #error x = input.split('{}') #this works but not the desired OP 所需OP: ['print', 'hello world', ';x =

如何在python中的split()中指定多个分隔符

input = "print{hello world};x = 2 + 3;"
x = input.split('{') #this works but not the desired OP
x = input.split('{','}') #error 
x = input.split('{}') #this works but not the desired OP
所需OP:

['print', 'hello world', ';x = 2 + 3;']
您可以这样使用:

import re

my_input = "print{hello world};x = 2 + 3;"
re.split('{|}', my_input)
输出:

['print', 'hello world', ';x = 2 + 3;']

在这种情况下,您可以使用re.split
input.replace('}','{')。split('{')
您可以将预期的输出添加到问题中。您可以在此处查看您的问题。我认为re是为此目的而制作的,而不是同时拆分