【发布时间】:2015-11-07 16:26:52
【问题描述】:
我有大量格式相同的文本文件 (>1000)。
我感兴趣的文件部分类似于:
# event 9
num: 1
length: 0.000000
otherstuff: 19.9 18.8 17.7
length: 0.000000 176.123456
# event 10
num: 1
length: 0.000000
otherstuff: 1.1 2.2 3.3
length: 0.000000 1201.123456
我只需要定义变量的第二个实例的第二个索引值,在这种情况下是长度。有没有这样做的pythonic方式(即不是sed)?
我的代码如下:
with open(wave_cat,'r') as catID:
for i, cat_line in enumerate(catID):
if not len(cat_line.strip()) == 0:
line = cat_line.split()
#replen = re.sub('length:','length0:','length:')
if line[0] == '#' and line[1] == 'event':
num = long(line[2])
elif line[0] == 'length:':
Length = float(line[2])
【问题讨论】:
-
就是一个文件的全部内容?
-
不,每个文件有超过 10 个事件,但格式都相同。编辑:我改变了上面的文件格式。
标签: python regex string if-statement for-loop