The beauty of Python

David Asorey Álvarez forodejazz at yahoo.es
Thu Jun 19 12:22:54 EDT 2003


"Thomas Weholt \( PRIVAT \)" <2002 at weholt.org> wrote in message news:<yrEHa.7094$Hb.127911 at news4.e.nsc.no>...
> I'm trying to promote Python as an elegant, easy-to-read and intuitive
> language at work and to my programming-buddies. I thought it would be nice
> to have a webpage with examples of the beauty of Python, showing small
> snippets of code solving real-world problems or tasks. If anybody has any
> such examples to offer, I'd be very gratefull. And if it turns out to be any
> interest for this in this group, I'll collect the source-snippets and put
> them on the net.

A small and very useful script, almost for me:

#### opera2mozilla.py ####

#!/usr/bin/env python
"""This module converts the Opera Web Browser bookmarks file to a
Mozilla 's bookmarks file.
License: GPL 
Author: David Asorey Álvarez (forodejazz at yahoo.es)
"""
import os.path
def read_file_opera(file):
    try:
        opera = open(file, 'r')
        try:
            mozilla = open('bookmarks.html', 'w')
        except:
            print 'Unable to create Mozilla Bookmarks file.'
            return -1
        lineas = [ln.strip() for ln in opera.readlines()]
        mozilla.write('<META HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=UTF-8">\n')
        mozilla.write('<TITLE>Bookmarks</TITLE>\n')
        mozilla.write('<H1>Bookmarks</H1>\n\n')
        mozilla.write('<DL><p>\n')
        for x in range(len(lineas)):
            if lineas[x] == '#FOLDER':
                mozilla.write( '\t<DT><H3>%s</H3>\n\t<DL><p>\n' %
(lineas[x+1][5:], ) )
            elif lineas[x] == '#URL':
                mozilla.write( '\t\t<DT><A HREF="%s">%s</A>\n' %
(lineas[x+2][4:], lineas[x+1][5:]) )
            elif lineas[x] == '-':
                mozilla.write('</DL><p>\n')
        mozilla.close
        opera.close
        print 'Exported bookmarks to file "bookmarks.html"'
        raw_input('Hit <ENTER> to quit.')
        return 0
    except:
        print 'Unable to open the file "%s"\nPlease, check your
input.' % file
        return -1


if __name__ == '__main__':
    mensaje = """Write the full path to your Opera bookmarks file.
The name is something like 'Opera6.adr'
"""
    myfile = os.path.normpath(raw_input(mensaje))
    read_file_opera(myfile)

#### End of opera2mozilla.py ####

Corrections and suggestions are wellcome!

-- 
David Asorey Álvarez
forodejazz at yahoo.es
Madrid (Spain)




More information about the Python-list mailing list