Note that there are some explanatory texts on larger screens.

plurals
  1. POThe opposite of string.Template in Python
    primarykey
    data
    text
    <p>I know template can work like the following:</p> <pre><code>x = Template(" Coordinates; $o1;$o2;$o3;\n") y = x.substitute(o1 = 23, o2 = 108, o3 = 655) </code></pre> <p>and y will give me:</p> <pre><code>" Coordinates; 23;108;655;\n" </code></pre> <p>I am wondering if there is a way to do the reverse of this? something like my made up unpack:</p> <pre><code>x = Template(" Coordinates; $o1;$o2;$o3;\n") y = " Coordinates; 23;108;655;\n" z = x.unpack(y) </code></pre> <p>and have z return something like:</p> <pre><code>["23","108","655"] </code></pre> <p>any ideas? should i be using regular expressions instead?</p> <p><strong>EDIT:</strong> If using regular expressions how would i program for the following 3 lines to return the first number and the 6 trailing numbers?</p> <pre><code> 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" </code></pre> <p>I tried this on those and couldn't seem to get it working:</p> <pre><code>&gt;&gt;&gt; re.match('(\d+); Coord ;(\d+);(\d+);(\d+);(\d+);(\d+);(\d+)',a).groups() </code></pre> <p><strong>SOLUTION:</strong> Using regular expressions <a href="http://docs.python.org/howto/regex.html" rel="nofollow"><strong>tutorial</strong></a> (thanks ironchefpython):</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; 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 """ &gt;&gt;&gt; coord = re.compile("\D*(\d+)\D+([\d\.]+)\D+([\d\.]+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)") &gt;&gt;&gt; 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')] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload