我有这个代码:
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"
我的问题是:我可以使用多个分隔符进行分割,而不是先替换,然后再进行分割吗?
使用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)