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...