Posts

Showing posts with the label re

Python re module's stuff # 1

When you do something with aws, then you need only ip-address from something like this $ ec2-1-2-3-4.ap-southeast-1.compute.amazonaws.com I used to deal with it like split something then concat with something. Now I just found something cool in python's re module. >>> import re >>> ec2_ip = re.search(r'ec2-(\d+)-(\d+)-(\d+)-(\d+), ).groups() so I get >>> print ec2_ip '1.2.3.4' yaha, love this.

Cools script

Never seen short code to get some page (python) >>print __import__('urllib').urlopen('http://google.com').read() ----> damn cools (other may have , but i just like this :)) note import re str_var = re.sub(r'\s',' ', str_var) # may be its make u a easy life

Using re and elementtree

import re import xml.etree.ElementTree as ET def extract(stream): r = re.compile('\}(?P \w+)') tree = ET.fromsrting(stream) dfeed = {} dfeed[r.search(tree.tag).group(1)] = None # feed l0_node = dict() for node in tree: # l0 l1_children = node.getchildren() l1_node = dict() if l1_children: # l1 for l1_child in l1_children: l2_children = l1_child.getchildren() l2_node = dict() if l2_children: # l2 for l2_child in l2_children: l3_children = l2_child.getchildren() l3_node = dict() if l3_children: #l3 for l3_child in l3_children: l3_node[r.search(l3_child.tag).group(1)] = l3_child.text l2_node[r.search(l2_child.tag).group(1)] = l3_node else: l2_node...