开发者

Find associated value to a keyword in file with python

Sorry for the beginner python question, but I cant find anywhere how to do this, so bear with me.

I am trying to extract the values from a file containing keyword followed by a value: Example:

length 95 width 332 length 1253 length 345 width 22

How do I extract all the values assosiated with the keyword "length" for exampl开发者_运维技巧e?


>>> s = 'length 95 width 332 length 1253 length 345 width 22'
>>> import re
>>> re.findall(r'length (\w+)', s)
['95', '1253', '345']

This would do too, but it has additional constraints:

>>> sp = s.split()
>>> [sp[i+1] for i, l in enumerate(sp) if l == 'length']
['95', '1253', '345']


The "re" module should do for you. Otherwise, if you know the (possibly few) keywords in your (possibly short) input, go the rough way and do some string slicing.


the following code may help you. I haven't tested it so you may have to do some adjustment, but it should give you the basic idea

import re

f = open('filename', 'r')
for line in f.readlines():
  for m in re.finditer('length\s+(?P<number>\d+)', line):
    print m.group('number')


You will have to split the contents, for example like so (if you're reading the whole file anyway):

with open("filename", "rb") as f:
    l = f.read().split()
    valuesForLengthKeyword = tuple(int(l[i+1])
                                   for i in xrange(0, len(l), 2)
                                   if l[i] == "length")

print valuesForLengthKeyword

This will print a tuple like (95, 1253, 345).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜