« November 2009
SunMonTueWedThuFriSat
1
2
3
4
5
6
7
8
9
10
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
     
       
Today
XML

Neat blogs

Navigation

Editing

Powered by Roller Weblogger.

statcounter.com

clustrmaps.com

Locations of visitors to this page

technorati.com

20081007 Tuesday October 07, 2008
The simple version of the old perl script

My first pass at a Python version of An old perl script reveals my inner C programmer. I've restricted the program to the simple version which does not generate the column name as local variables - first I want to get my proof of concept correct:

#!/usr/bin/python

for line in open("resume.txt"):
        line.lstrip()
        if line.startswith("!") or line.startswith("#"): continue
        (started, ended, title, company, description) = line.split(",")
        print "%s - %s: %s for %s\n\t%s\n\n", started, ended, title, company, description

It looks like it will reformat, but I've messed up the print statement:

> ./simple_1.py
%s - %s: %s for %s
        %s

1/05 present Staff Engineer Software Sun Microsystems NFS development

%s - %s: %s for %s
        %s

6/01 12/05 File System Engineer Network Appliance WAFL and NFS development

%s - %s: %s for %s
        %s

4/01 6/01 Manager Network Appliance Manager of Engineering Internal Test

%s - %s: %s for %s
        %s

10/99 4/01 System Administrator Network Appliance Perl hacker and filer administrator

I.e., I treated print like a C printf. Okay, I can try again with this one:

#!/usr/bin/python

for line in open("resume.txt"):
        line.lstrip()
        if line.startswith("!") or line.startswith("#"): continue
        (started, ended, title, company, description) = line.split(",")
        print "%s - %s: %s for %s\n\t%s\n\n" % (started, ended, title, company, description)

And get more of what I want to see:

> ./simple.py
1/05 - present: Staff Engineer Software for Sun Microsystems
        NFS development



6/01 - 12/05: File System Engineer for Network Appliance
        WAFL and NFS development



4/01 - 6/01: Manager for Network Appliance
        Manager of Engineering Internal Test



10/99 - 4/01: System Administrator for Network Appliance
        Perl hacker and filer administrator

I'm getting an extra line I don't want and I have to hard code the file to process. I can easily fix these both up:

#!/usr/bin/python

import sys

for line in open(sys.argv[1]):
        line.lstrip()
        if line.startswith("!") or line.startswith("#"): continue
        (started, ended, title, company, description) = line.split(",")
        print "%s - %s: %s for %s\n\t%s\n" % (started, ended, title, company, description)

Feeling adventurous

Okay, with this simple example, I could get rid of the names in Perl and make it really simple. Can I do so in Python?

#!/usr/bin/python

import sys

for line in open(sys.argv[1]):
        line.lstrip()
        if line.startswith("!") or line.startswith("#"): continue
        print "%s - %s: %s for %s\n\t%s\n\n" % line.split(",")

No, not as I have tried:

> ./simple2.py resume.txt
Traceback (most recent call last):
  File "./simple2.py", line 8, in 
    print "%s - %s: %s for %s\n\t%s\n\n" % line.split(",")
TypeError: not enough arguments for format string

I've got a type error, hmm, I'm going to try this by hand:

> python
>>> st1 = "This is the radio clash!"
>>> st1.split()
['This', 'is', 'the', 'radio', 'clash!']
>>>

So I have a '[]' instead of a '()'. What does that mean? It means I have a list versus a tuple. And I find a converter called strangely enough, tuple:

        print "%s - %s: %s for %s\n\t%s\n\n" % tuple(line.split(","))

And that works.


Originally posted on Kool Aid Served Daily
Copyright (C) 2008, Kool Aid Served Daily

Trackback URL: http://blogs.sun.com/tdh/entry/the_simple_version_of_the
Comments:

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed