Presenting source code

I love Python

Simple and straight forward

import string
# This module will transform a commaseparated file to xml: makeXML
# title,author,publisher,year,isbn,pages,course,category,comment

# a template for a xml-fragment
XMLFragment="""
<book isbn="%s" pages="%s">
      <title>%s</title>
      <course>%s</course>
      <category>%s</category>
      <author>%s</author>
      <publisher>%s</publisher>
      <year>%s</year>
      <comment>%s</comment>
</book>
"""

# a template for a complete xml-file
XMLFile="""<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="makelist.xslt"?>
<booklist>
%s
</booklist>
"""

def getTextFile(filename):
    try:
        file=open(filename,'r')
        intext=file.read()
        file.close()
        return intext
    except:
        print 'Error reading file ',filename
        return ''

def storeTextFile(filename,txt):
    try:
        outfile=open(filename,'w')
        outfile.write(txt)
        outfile.close()
    except:
        print 'Error writing file ',filename
        

def makeXML(filename='c:\\articles\\ml\\python\\bokliste.txt'):
    # read text file
    text=getTextFile(filename)
    if(text==''):
        return
    content=''
    # pick lines
    lines=text.split('\n')
    for line in lines:
        line.strip()
        if(len(line)<2):
            continue
        if(line[0:2]=='//'):
            continue
        # find parts
        pcs=line.split(',')
        if(len(pcs)!=9):
            print 'ignore:' , line
        content+=XMLFragment%(pcs[4],pcs[5],pcs[0],pcs[6],pcs[7],pcs[1],pcs[2],pcs[3],pcs[8])
    
    storeTextFile(filename.replace('.txt','.xml'),XMLFile%content)
../common/prev.gif ../common/home.gif ../common/next.gif

Valid XHTML
Bygget med WXT : 02.nov.2005