String Methods and Regular Expressions
Contents
This task link on the end of the page will use string methods and regular expressions in Python to do some automated text processing. The task also provides an introduction to a very simple CGI process.
The task: reformatting NWS text products. This is a python script that reformats raw NWS text products.
Some CGI basics
If you dabbled with my html examples within htmlex.tar, available from the HTML Tutorial, then you may already have a working CGI script at your website. You may have one like mine: mesograb.cgi.
In order to learn the essence of CGI, you are invited to play with that script. Here are some simple experiments you can do.
From your Linux command line, execute the script mesograb.cgi, just as you would any other python script. Notice you see the HTML tags that you do not see directly when the output is viewed through your browser.
Notice that mesograb.cgi accepts a command line argument, for example: mesograb.cgi ALTU
mesograb.cgi also accepts arguments when executed through a browser: just join things with a "?" rather than " ": mesograb.cgi?ALTU.
The first line that the CGI script prints should be Content-type: text/html followed by a blank line. I have read that the client computer demands this line. This seem reasonable to me. Yet when I send a text file or html file over the web, I do not need to put that line in my file, and the client side stays happy. So perhaps the server (Gentry) normally prepends the Content-type line to files from my website.
- Can we claim CGI is easy? Usually so, but sometimes not. There is sometimes a big chasm to cross between getting a Python script to function for a Linux command line, with permissions set for the owner of the program, and getting Apache to run the same program at the request of a remote user. Hopefully that chasm will be effortlessly crossed in the CGI task here.
Links about Elementary CGI with Python
Regular Expressions and String methods in Python
Regular expressions are essential to almost every programmer and every workstation user. See Using Regular Expressions for an overview, with a little bit of history about the origin of the phrase "regular expressions"
String manipulation in python Not all string manipulation requires regular expressions. Python has string methods for many simples tasks. Elementary use of regular expressions is found at the end of the article.
http://www.python-course.eu/re.php A gentle introduction to python's re module
Regular Expressions from Dive Into Python.
re — Regular expression operations official doc
vim regular expressions, for those who might need it:
vim regular expressions 101 vim (or gvim) regex...explains syntax that is slightly different from perl's