[Tutor] New to Python and programming and need suggestion

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Sun, 7 Jan 2001 03:32:11 -0800 (PST)

On Sat, 6 Jan 2001, Curtis Horn wrote:

> Hello, I'm new to programming and python (just bought Learn to
> Program Using Python by Alan Gauld, I'm on ch 14).  I feel like
> I have learned alot so far, and I look forward to finally bieng able
> to do something with all the ideas I get.

Cool!  Let's see what you want to do:

> 1. There is a text file that I want to monitor for certain key words,
> it is organized by date time msg.  The msg can be more than one word,
> and each date time msg is on a seperate line. There are several
> computers that need to be monitored in this way.

Ok, it sounds like you may find the string splitting operations really
useful to parse out the date, time, and message.  Let's say that the text
file separates each field with a space.  Then, we could split each line up
like this:

    date = string.split(line, ' ')[0]
    time = string.split(line, ' ')[1]
    msg = string.join(string.split(line, ' ')[2:], ' ')

Regular expressions might be more appropriate and nicer to use.  Now that
I think about it, the above code might be buggy too --- you'll probably
need to experiment a bit.  *grin*

> 2. For the lines with those keywords I'd like to have them uploaded
> onto a database running on a server.

This isn't too hard if you go with SQL --- you'll be executing an
"insert" query on a database connection that you make.

SQL database code has the following structure:

    Make a connection.
    Get a "cursor" from the connection: something that lets you execute
    Execute some sort of query.
    Fetch results if you need it.

Here's sample (untested) code that assumes that we're using MySQL:

    import MySQLdb
    connection = MySQLdb.connect(db='test')
    cursor = connection.cursor()
    cursor.execute("select first_name, last_name from names")
    names = cursor.fetchall()

This shows that SQL stuff isn't too complicated.  If you're going to do
SQL inserts, you'll definitely want to learn about string interpolation:


The stuff about '%' is the stuff on string interpolation.  With '%', you
can build up your SQL statements pretty easily:

    fname = 'Curtis'
    lname = 'horn'
    stmt = """insert into names (first_name, last_name) 
              values ('%s', '%s')" % (fname, lname)

is one example.

> 3. I'd like people on the LAN to be able to look on a intranet page and
> access the data on the database, visually and if they need more detail
> with text.

Ok, so that sounds like you'll be using either Python, PHP, or something
else to present the database information.  This should be very doable
too.  You might want to look at some of the CGI tutorials here:


> maybe there is another way to monitor text files remotly with a web
> page other than using a database as an in-between?

Not quite sure --- it really depends on what sort of access the server has
with those client computers.

> since I'm new to python, some things are not clear to me, like how to
> go about having data sent to the database, and how to access the info
> with a web page. I'm assuming it can be done with python and some
> other tools, like maybe gadfly or mySQL and a web server?

Going with SQL will work, and it should be fun as well.

You'll probably want to look here for database stuff:


The important stuff there is the DB-API 2.0 --- most database drivers
support this interface --- and the Database Modules link.  If you go with
MySQLdb, you'll want to use this link:


Whew!  Lots of stuff here; forgive me for rambling!  I feel a little
uncomfortable about just mentioning these topics; is there anything in
particular that you might want to start on?  The project that you're
planning ties together several topics, but if you take it slowly, it
shouldn't be overwhelming.

I hope this helps!