Squid
Squid

Reputation: 1

python regular expression, wrong results

I am new to python and regular expressions. I am currently trying to make a program that reads the contents of the file below and get specific parameters and max_speeds within the sections. Under each SECTION:#, the parameters are all indented (TAB) until the next SECTION:#

[SECTION:3]
      paramter = 3
      state = AZ
      car = toyota
      max_speed = 90.000
      any_pets = yes
[SECTION:13]
      paramter = 10
      state = NY
      car = honda
      max_speed = 120.000
      any_pets = yes
[SECTION:85]
      paramter = 31
      state = TX
      car = kia
      max_speed = 30.000
      any_pets = no

This is my code:

import re
file = open('file.txt').readlines()
file_str = str(file)

for lines in file_str:
     myreg = re.compile(r'(?<=SECTION:13).+(max_speed\s\=\s\w+)')
     myreg1 = myreg.search(lines)
     print myreg1.group(1)

The problem is that the results are always wrong...it's as if the regular expression always matches the results of the last section.

Please let me know what am i doing wrong and what would be the best way of doing it. Thank you!

Upvotes: 0

Views: 939

Answers (3)

pinkdawn
pinkdawn

Reputation: 1033

To deal with indentation under the sections while using the ConfigParser module, just using following code:

from ConfigParser import ConfigParser

class fp():
    def __init__(self, filename):
        self.fileobj = open(filename)

    def readline(self):
        return self.fileobj.readline().lstrip()

f = fp('e:/file.txt')
config = ConfigParser()
config.readfp(f)
print config.get('SECTION:3', 'state')

Upvotes: 0

pinkdawn
pinkdawn

Reputation: 1033

you should try some like this: (I'm not running and test the code, make it run yourself)

import re    
pattern = '(?<=SECTION:13).+(max_speed\s\=\s\w+)'
mattches = re.findall(pattern, '\n'.join(open('file.txt').readlines()))
print mattches

Upvotes: 0

Ned Batchelder
Ned Batchelder

Reputation: 375584

You have a number of problems. First, read lines in a file like this:

with open('file.txt') as f:
    for line in f:
        # process each line.

The way you are reading lines, you create a list with readlines, then make it a string with str, which will give you data like "['line1\n', 'line2\n']". Then iterating over that string will give you each character in turn.

But you probably don't need to read the file yourself at all. The built-in module ConfigParser will parse these files for you directly, give it a look.

Upvotes: 3

Related Questions