我的文本内容是这样的:
newmtl FenceWood
d 1
Ns 0.628803
Ni 0
newmtl FenceBase
d 1
Ns 0.845842
newmtl ArchWallBaseTrim
d 1
Ns 0.721881
Ka 0 0 0
newmtl Plaster3
d 1
Ns 0.756646
Kd 0.5 0.5 0.5
我想以空行分割输出:
for i in f:
print i
我需要这样的结果
0:
newmtl FenceWood
d 1
Ns 0.628803
Ni 0
1:
newmtl FenceBase
d 1
Ns 0.845842
2:
newmtl ArchWallBaseTrim
d 1
Ns 0.721881
Ka 0 0 0
3:
newmtl Plaster3
d 1
Ns 0.756646
Kd 0.5 0.5 0.5
from __future__ import with_statement # for Python25txt_file = 'txt.txt'i = 0lines = []with open(txt_file, 'rU') as f:
for line in f:
if line.isspace():
print '%d:' % i
print ''.join(lines)
lines = []
i += 1
else:
lines.append(line)