views:

270

answers:

1

I have the following code in .py file:

import re

regex = re.compile(
    r"""ULLAT:\ (?P<ullat>-?[\d.]+).*?
    ULLON:\ (?P<ullon>-?[\d.]+).*?
    LRLAT:\ (?P<lrlat>-?[\d.]+)""", re.DOTALL|re.VERBOSE)

I have the data in .txt file as a sequence:

QUADNAME: rockport_colony_SD
RESOLUTION: 10 ULLAT: 43.625 ULLON:
-97.87527466 LRLAT: 43.5 LRLON: -97.75027466 HDATUM: 27 ZMIN: 361.58401489 ZMAX: 413.38400269 ZMEAN: 396.1293335 ZSIGMA: 12.36359215 PMETHOD: 5 QUADDATE: 20001001

How can I use the Python -file to process the .txt -file?

I guess that we need a parameter in the .py file, so that we can use a syntax like in terminal:

$ py-file file-to-be-processed

This question was raised by the post here.

+7  A: 

You need to read the file in a the search the contents using the regular expression. The sys module contains a list, argv, which contains all the command line parameters. We pull out the second one (the first is the file name used to run the script), open the file, and then read in the contents.

import re
import sys

file_name = sys.argv[1]
fp = open(file_name)
contents = fp.read()

regex = re.compile(
    r"""ULLAT:\ (?P-?[\d.]+).*?
    ULLON:\ (?P-?[\d.]+).*?
    LRLAT:\ (?P-?[\d.]+)""", re.DOTALL|re.VERBOSE)

match = regex.search(contents)

See the Python regular expression documentation for details on what you can do with the match object. See this part of the documentation for why we need search rather than match when scanning the file.

This code will allow you to use the syntax you specified in your question.

Andrew Wilkinson