[Tutor] Re: Re: Translating to Python [perl --> python]

Kyle Babich Kyle Babich" <kb@kb5.org
Tue, 2 Jul 2002 16:12:17 +0000


It works!  :)  Thank you.

My next question is how do I open a file for appending vs.
overwritting?  I don't see this in any tutorials.  (or maybe I'm just
missing it)

On Tue, 2 Jul 2002 09:30:22 -0500, "Derrick 'dman' Hudson"
<dman@dman.ddts.net> said:
> On Tue, Jul 02, 2002 at 01:59:40PM +0000, Kyle Babich wrote:
> | Ok, well then something is still wrong because I'm still getting
> syntax
> | errors that I can't figure out.  Is there a python tutorial that is
> | more like cgi101.com (only python)?  (Ie. including source codes,
> live
> | working examples, and different sections for different commands)
> 
> http://python.org/doc/current/tut/tut.html
> 
> It covers the python language.  Working with CGI is trivial once you
> know the language -- just use the same environment variables as always
> (found in the 'os.environ' dictionary) and use the cgi.FieldStorage
> class to parse form submissions.
>  
> | Btw, here is the code again:
> | 
> | #!/usr/bin/env python
> | 
> | import cgi
> | 
> | print "Content-type: text/html\n\n"
> | 
> | form = cgi.FieldStorage()
>   try:
>   ^^^^
>       c = form['c'].value
>   ^^^^
> | except KeyError :
>       c = ""
>   ^^^^
> | content = c
> | if c == 'abc':
>       content = "abc123"
>   ^^^^
> | elif c == 'def':
>       content = "def456"
>   ^^^^
> | else:
>       print "error: content failed\n"
>   ^^^^
> 
> | It says there is an error near except KeyError:
> 
> The 'except' keyword can only be used after a 'try:'.  Just like in
> perl you can't put "else" all over the place, only after an "if".
> Also, you need to indent the blocks of compound statements.  If, in
> perl, it would have had curly braces around it, indent it.
> 
> | but I've never used python before so I don't know what to change.
> 
> Read Guido's tutorial.  It is very easy to read if you already know
> the basics of programming (which you do if you've used perl before).
> 
> HTH,
> -D
> 
> -- 
> 
> A kindhearted woman gains respect,
> but ruthless men gain only wealth.
>         Proverbs 11:16
>  
> http://dman.ddts.net/~dman/
>