python 通过空格和换行符分割字符串

Pet*_*hao 4 python split

我有这个代码:

f1=open('test.txt','r')
d={}
line = f1.read().replace('\n',' ')
line2= line.split("\n")
Run Code Online (Sandbox Code Playgroud)

line = "This is line1\nthis isline2\nthis is line3"

我的问题是:我可以使用多个分隔符进行分割,而不是先替换,然后再进行分割吗?

hee*_*ayl 5

使用re.split()

\n在和上拆分\t

In [23]: line = "This is line1\nthis isline2\tthis is line3"

In [24]: re.split(r'[\n\t]', line)
Out[24]: ['This is line1', 'this isline2', 'this is line3']
Run Code Online (Sandbox Code Playgroud)