views:

42

answers:

4

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 example?

+1  A: 

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')
PierrOz
this is not Python.
SilentGhost
yes, yes... sorry for the typo... after some checks the script above is now working
PierrOz
+1  A: 

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.

Mattia Gobbi
+1  A: 
>>> 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']
SilentGhost
What does "(\w+)" in the first code snip do?
Theodor
@Theo: collects all consecutive word characters.
SilentGhost
A: 

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).

AndiDog