我有一个非常长的文本文件,我想将其拆分为较小的文件。看起来像:*** 200302 abcdfg *** 200303 fasafafd *** 200304 dajhskjsd
我希望将***之间的内容另存为(1.txt,2.txt,3.txt ...)类型的新文件
我尝试了另一个讨论线程(How can I split a text file into multiple text files using python?)中发布的建议
我还尝试使用下面显示错误的代码。错误在第6行(SyntaxError:行继续符后出现意外字符)。
with open ('filename.txt','r') as fo:
op=''
start=0
cntr=1
for x in fo.read().split(*\n*):
if (x=='***'):
if (start==1):
with open (str(cntr)+'.txt','w') as opf:
opf.write(op)
opf.close()
op=''
cntr+==1
else:
start=1
else:
if (op==''):
op = x
else:
op=op + '\n' + x
fo.close()