« November 2009
SunMonTueWedThuFriSat
1
2
3
4
5
6
7
8
9
10
11
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

20081009 Thursday October 09, 2008
Another reader suggestion for the Python script

So Justin suggested:

try

template =  """"%(started)s - %(ended)s: %(title)s for      
%(company)s
    %(jobdesc)s"""


print template % row

I'm trying to capture what he put in, the line wrap on the first 'template' line is probably the comment system.

So I entered this, making a reformat tweak, as:


template = """"%(started)s - %(ended)s: %(title)s for %(company)s %(description)s"""

if __name__ == "__main__":
        for row in main(sys.argv[1],'!'):
                print template % row

And I got the wrong output. The error is of course mine:

> ./justin.py r4.txt
"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 shouldn't have joined the lines in the 'template' format. But why the extra '"'?

Ahh, Justin had one in his entry.

Okay, so the new changes work, here is the full program:

#!/usr/bin/env python

import csv, sys

def main(dfile,format,delimiter=","):
        db=open(dfile,'U')
        start=0
        for line in db:
                if line.startswith(format):
                        db.seek(start+len(format))
                        return csv.DictReader(db,delimiter=delimiter)
                else:
                        start+=len(line)+(len(db.newlines)==2) #windows hackery
        raise "There is no %s header line in %s" % (format,dfile)

template = """%(started)s - %(ended)s: %(title)s for %(company)s
        %(description)s"""

if __name__ == "__main__":
        for row in main(sys.argv[1],'!'):
                print template % row

And now I need to go figure out what Justin did...

Okay, the '%(name)s' has to be a formatting option. Can I duplicate it?

>>> for row in csv.DictReader(file("r4.txt")):
...     print "%(title)s" % row
...
Staff Engineer Software
File System Engineer
Manager
System Administrator

And now I do understand it - 3.6.2 String Formatting Operations:

A conversion specifier contains two or more characters and has the following components, which must occur in this order:

   1. The "%" character, which marks the start of the specifier.
   2. Mapping key (optional), consisting of a parenthesised sequence of characters (for example, (somename)). 

So, this code would be good for printing, but not necessarily for doing more complex manipulations.

By the way, I'm having fun figuring this stuff out...


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

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

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed