From: Νίκος on 7 Aug 2010 13:29 Hello guys! Need your precious help again! In every html file i have in the very first line a page_id fro counetr countign purpsoes like in a format of a comment like this: <!-- 1 --> <!-- 2 --> <!-- 3 --> and so on. every html file has its one page_id How can i grab that string representaion of a number from inside the .html file using regex and convert it to an integer value? # ============================== # open current html template and get the page ID number # ============================== f = open( '/home/webville/public_html/' + page ) #read first line of the file firstline = f.readline() page_id = re.match( '<!-- \d -->', firstline ) print ( page_id )
From: Νίκος on 7 Aug 2010 13:31 i also dont know what wrong with this line: host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] hostmatch = re.search('cyta', host) if cookie.has_key('visitor') != 'nikos' or hostmatch is None: # do stuff the 'stuff' never gets executed, while i ant them to be as long as i dont have regex match!
From: MRAB on 7 Aug 2010 14:24 Νίκος wrote: > Hello guys! Need your precious help again! > > In every html file i have in the very first line a page_id fro counetr > countign purpsoes like in a format of a comment like this: > > <!-- 1 --> > <!-- 2 --> > <!-- 3 --> > > and so on. every html file has its one page_id > > How can i grab that string representaion of a number from inside > the .html file using regex and convert it to an integer value? > > # ============================== > # open current html template and get the page ID number > # ============================== > > f = open( '/home/webville/public_html/' + page ) > > #read first line of the file > firstline = f.readline() > > page_id = re.match( '<!-- \d -->', firstline ) > print ( page_id ) Use group capture: found = re.match(r'<!-- (\d+) -->', firstline).group(1) print(page_id)
From: MRAB on 7 Aug 2010 14:27 Νίκος wrote: > i also dont know what wrong with this line: > > host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] > > hostmatch = re.search('cyta', host) > > if cookie.has_key('visitor') != 'nikos' or hostmatch is None: > # do stuff > > the 'stuff' never gets executed, while i ant them to be as long as i > dont have regex match! Try printing out repr(host). Does it contain "cyta"?
From: Νίκος on 7 Aug 2010 14:48 On 7 ÎÏγ, 21:27, MRAB <pyt...(a)mrabarnett.plus.com> wrote: > ÎÎ¯ÎºÎ¿Ï wrote: > > i also dont know what wrong with this line: > > > host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] > > > hostmatch = re.search('cyta', host) > > > if cookie.has_key('visitor') != 'nikos' or hostmatch is None: > >    # do stuff > > > the 'stuff' never gets executed, while i want them to be as long as i > > dont have regex match! > > Try printing out repr(host). Does it contain "cyta"? Yes it does contain it as print shown! is something wrong with this line in logic or syntax? if cookie.has_key('visitor') != 'nikos' or re.search('cyta', host) is None: # do database stuff
|
Next
|
Last
Pages: 1 2 3 Prev: Sequential Object Store Next: cscope like source code navigation for python code |