syntax-error?

flynt flynt at gmx.ch
Fri Nov 23 16:20:28 EST 2001


Stefan Antoni wrote:
> 
> I got the following script and it produces a syntax error which drives
> me crazy. it says:
> 
> File "<string>", line 24
>   j = 0 # a counter
>   ^
> SyntaxError: invalid syntax
> 
> Why?
> 
> is j = 0 invalid?
> 
> here's the script (anybody who can help me?):
> 
> import httplib
> 
> # request the page and get it
> def GetUrl(ServerAdr, PagePath):
>         http = httplib.HTTP(ServerAdr)
>         http.putrequest('GET', PagePath)
>         http.putheader('Accept', 'text/html')
>         http.putheader('Accept', 'text/plain')
>         http.endheaders()
>         httpcode, httpmsg, headers = http.getreply()
>         if httpcode != 200:
>                 raise "Could not get document: Check URL and Path"
>         doc = http.getfile()
>         data = doc.read()
>         doc.close()
>         return data
> 
> # parse the page and return the content between the start and end token
> def ExtractData(in_string, start_line, end_line):
>     lstr = in_string.splitlines()
>         j = 0 # a counter
>         for i in lstr:
>                 j = j+1
>                 if i.strip() == start_line: slice_start = j # find slice start
>                 elif i.strip() == end_line: slice_end = j   # find slice end
>         return lstr[slice_start:slice_end] # return the slice
> 
> # handle the returned stuff and generate a new page
> def main():
>         # parameter and constants
>         ServerAdr = 'www.onlinekosten.de'
>         PagePath = '/'
> 
>         StartLine = '<head>'
>         EndLine   = '</head>'
> 
>         Head1 = ''
>         Head2 = ''
> 
>         Foot = ''
> 
>         # call functions
>         RawData = GetUrl(ServerAdr, PagePath)
>         v = ExtractData(RawData, Startline, EndLine)
> 
>         # return result and construct page
>         print Head1.strip() + ServerAdr.strip() + Head2.strip()
>         for i in v:
>                 print i.strip()
>         print Foot.strip()
> 
> if __name__ == '__main__':
>         main()
> 
> --
> thx in advance,
> Stefan Antoni

Hi Stefan

I look at the lines:

 def ExtractData(in_string, start_line, end_line):
     lstr = in_string.splitlines()
         j = 0 # a counter
         for i in lstr:
                 j = j+1

What is *not* clear to me:
1. Is your intendation consistent (only tabs or only always the same
number of spaces ?
2. after *lstr = in_string.splitlines()* you intend again. Why ? the
following line *j = 0 # a counter* belongs to the same block as one line
higher. So do you have an intendation too much ?

Regards

--- Flynt




More information about the Python-list mailing list