3

I know template can work like the following:

x = Template("  Coordinates;     $o1;$o2;$o3;\n")
y = x.substitute(o1 = 23, o2 = 108, o3 = 655)

and y will give me:

"  Coordinates;     23;108;655;\n"

I am wondering if there is a way to do the reverse of this? something like my made up unpack:

x = Template("  Coordinates;     $o1;$o2;$o3;\n")
y = "  Coordinates;     23;108;655;\n"
z = x.unpack(y)

and have z return something like:

["23","108","655"]

any ideas? should i be using regular expressions instead?

EDIT: If using regular expressions how would i program for the following 3 lines to return the first number and the 6 trailing numbers?

   a = "   123;  Coord   ;  19.1335;   3.5010;  1; 3; 8; 4"
   b = "    17;  Coord   ;  15.2940;  13.5010;  3; 1; 8; 8"
   c = "     5;  Coord   ;  19.1345;   0.6200;  1; 1; 7; 8"

I tried this on those and couldn't seem to get it working:

>>> re.match('(\d+);  Coord   ;(\d+);(\d+);(\d+);(\d+);(\d+);(\d+)',a).groups()

SOLUTION: Using regular expressions tutorial (thanks ironchefpython):

>>> import re
>>> text = """
       123;  Coord   ;  19.1335;   3.5010;  1; 3; 8; 4
        17;  Coord   ;  15.2940;  13.5010;  3; 1; 8; 8
         5;  Coord   ;  19.1345;   0.6200;  1; 1; 7; 8
    """
>>> coord = re.compile("\D*(\d+)\D+([\d\.]+)\D+([\d\.]+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)")
>>> coord.findall(text)
[('123','19.1335','3.5010','1','3','8','4'),('17','15.2940','13.5010','3','1','8','8'),('5','19.1345','0.6200','1','1','7','8')]
1
  • 4
    Regular expressions are the opposite of string templates Commented Jun 17, 2011 at 20:55

3 Answers 3

5
>>> import re
>>> y="  Coordinates;     23;108;655;\n"
>>> re.match("  Coordinates;     (\d+);(\d+);(\d+);\n", y).groups()
('23', '108', '655')

You can also do this to get a dict of the values

>>> re.match("  Coordinates;     (?P<o1>\d+);(?P<o2>\d+);(?P<o3>\d+);\n", y).groupdict()
{'o3': '655', 'o2': '108', 'o1': '23'}
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent answer, could you please help me out with my edit? I'm pretty new to regular expressions and can't seem to get it to work.. i think it has something to do with the unpredictable whitespace.
2

Regarding your edit, if you're looking to work with regular expressions, I highly recommend looking at a tutorial; without some guidance regular expressions look like incompressible garbage, and even though other people can write your regular expressions for you, you should at least understand what they are doing.

That being said,

>>> re.match(r"\D*(\d+)\D+([\d\.]+)\D+([\d\.]+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)", 
             "   123;  Coord   ;  19.1335;   3.5010;  1; 3; 8; 4").groups()
('123', '19.1335', '3.5010', '1', '3', '8', '4')

5 Comments

that does look like garbage haha.. I'm off to read the tutorial, thanks for the link.
This might be a bit easier to read, it looks for not-digits, followed by digits, followed by not-digits, followed by digits, followed by not-digits, followed by digits, etc....
it is but it actually returned ('123', '19', '1335', '3', '5010', '1', '3')
sorry, I made a typo when I copied it from the interpreter... try it now.
excellent, thanks for your help.. regular expressions are quite complex, looks like i'll be reading this tut for a while :)
0

What you can do is create a class with your custom templating identifier and then use regular expression to identify those identifiers from the objects you create. You wish to create a dictionary to store those identifier value pairs and then in your (custom) methods such as unpack , just give the values of the identifiers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.