Re: Parsing files -- pyparsing to the rescue?

2006-01-16 Thread Allan Zhang
Try this

code
=
import re
p = re.compile(r'([^<]*)<')
s = open("file").read()
m = re.search(p, s)
if m: res = m.groups()[0]
res = res.lstrip("\n")
res = res.rstrip("\n")
print res


result:
===
%python parser.py
Sys Data
Sys-Data
asdkData
Data
%

Thanks
Allan
"rh0dium" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Hi all,
>
> I have a file which I need to parse and I need to be able to break it
> down by sections.  I know it's possible but I can't seem to figure this
> out.
>
>The sections are broken by <> with one or more keywords in the <>.
> What I want to do is to be able to pars a particular section of the
> file.  So for example I need to be able to look at the SYSLIB section.
> Presumably the sections are
>
>
> 
> Sys Data
> Sys-Data
> asdkData
> Data
> 
> Data
> Data
> Data
> Data
> 
> Data
> Data
> Data
> Data
> 
> Data
> Data
> Data
> Data
> 
>
> So if I wanted to break them down..
>
> Sections are broken down by this..
>
> secH=pyparsing.LineStart() + pyparsing.Suppress(
> pyparsing.Literal("<")) +
> pyparsing.OneOrMore(pyparsing.Word(pyparsing.alphanums)) +
> pyparsing.Suppress( pyparsing.Literal(">"))
>
> But how do I say that  stops at the start of the next
> ?
> 


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sockets

2006-01-20 Thread Allan Zhang
>
>> the following code works perfectly
>>   import socket, sys
>>   s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>   s.connect(("www.python.org", 80))
>>   s.send("GET")
here, You need to speak HTTP protocol. I would suggest to change
s.send( "GET / HTTP/1.0\x0d\x0a\x0d\x0a" )


>>   while 1:
>> buf = s.recv(1000)
>> if not buf:
>> break
>> sys.stdout.write(buf)
>>   s.close()
>>
>>   but the following code does not work
>>
>> import socket, sys
>>   s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>   s.connect(("http://www.google.co.in/search?hl=en&q=india&meta=";, 80))
>>   s.send("GET")
It does not work is because you did not speak HTTP

s.connect( ( www.google.co.in, 80) )
s.send( "GET /search?hl=en&q=india&meta= HTTP/1.0\x0d\x0a\x0d\x0a" )



>>   while 1:
>> buf = s.recv(1000)
>> if not buf:
>> break
>> sys.stdout.write(buf)
>>   s.close()
>>
>> the given url is the google search url for the string india.
>> can u suggest some way to access the google search result page
>> through SOCKETS.
>
 import urllib
 help(urllib)
>
> but this won't help; using scripts to scrape the google search page is
> a violation of their TOS.  for a proper way to do it, see:
>
>http://www.google.com/apis/
>
> or use yahoo's search service, which is a lot easier to use:
>
>http://developer.yahoo.net/search/index.html
>http://developer.yahoo.net/search/web/V1/webSearch.html
>
> 
>
>
> 


-- 
http://mail.python.org/mailman/listinfo/python-list