我想在python中将文件分割成多个文件。因此,我找到了findall函数来完成这项工作。
我的文件包含:
**05/02/2020
- Test PC
- Electricite
W=10
PUI=5
- Test MAPS
Nothing for now
- Date/Hours
DateTest=12h14
DateFinish=13h18
**05/02/2020
所以,我使用下面的代码用'-'分割该文件,但是分割得不是很好。
import re
def main():
with open('mesfile.log', 'r') as f:
data = f.read()
found = re.findall(r'\n*(- .*?\- .*?)\n*', data, re.M | re.S)
[open(str(i)+'.txt', 'w').write(found[i-1]) for i in range(1, len(found)+1)]
if __name__=="__main__":
main()
预期输出
File 1 contains
- Test PC
File 2 contains
- Electricite
W=10
PUI=5
File 3 contains
- Test MAPS
Nothing for now
File 4 contains
- Date/Hours
DateTest=12h14
DateFinish=13h18
**05/02/2020
您可以尝试以下方法:
with open('mesfile.log', 'r') as f:
data = [i.strip() for i in f]
contents = []
for line in data :
if line.startswith('-') :
contents.append([])
contents[-1].append(line)
for i,text in enumerate(contents) :
with open( 'file_%05d.txt' % i, 'w') as fout :
fout.write( '\n'.join( text ) )
用途:
res = []
#read content
with open(filename) as infile:
for line in infile:
line = line.strip()
if line.startswith("*") or not line:continue #skip empty line or date lines
if line.startswith("-"):
res.append([line])
else:
res[-1].append(line)
#write data to file
for idx, data in enumerate(res):
with open("file_{}".format(idx), "w") as infile:
infile.writelines(data)