2

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?

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
Theodor
  • 5,536
  • 15
  • 41
  • 55

4 Answers4

1

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
  • 7,653
  • 9
  • 48
  • 60
1

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.

MattiaG
  • 241
  • 1
  • 2
  • 9
1
>>> 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
  • 307,395
  • 66
  • 306
  • 293
0

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
  • 68,631
  • 21
  • 159
  • 205