Mine værktøjer
Du er her: Forside diverse filer til download mm. pysort.py
Handlinger tilknyttet webside

pysort.py

af adminSidst opdateret 27/09 2007 23:00

Sorter efter dato og tid.

Klik her for at hente fil

Størrelse: 1.8 kB - Filtype text/python-source

Indhold af fil-indholdsobjekt

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import datetime
Liste = """;14-11-2007,07:00 - 08:45,None,100
;21-11-2007,07:00 - 08:45,None,100
;12-12-2007,07:00 - 08:45,None,100
;19-12-2007,07:00 - 08:45,None,100
;26-12-2007,07:00 - 08:45,None,100
;02-01-2008,07:00 - 08:45,None,100
;20-02-2008,07:00 - 08:45,None,100
;27-02-2008,07:00 - 08:45,None,100
;05-03-2008,07:00 - 08:45,None,100
;12-03-2008,07:00 - 08:45,None,100
;12-09-2007,17:00 - 18:30,None,104
;19-09-2007,17:00 - 18:30,None,104
;26-09-2007,17:00 - 18:30,None,104"""

def mktid(dato="dd-mm-yyyy", tid="hh:mi" ):
    """returns datetime object"""
    dd , mm , aar = map( int, dato.split( "-" ) )
    hh, mi = map( int, tid.split( ":" ) )
    return datetime.datetime(aar, mm, dd, hh, mi)

def sort_dato( x, y ):
    """Compare dates in format dd-mm-yyy,hh:mi"""

    dato = x.split( "," )[ 0 ]
    tid  = x.split( "," )[ 1 ].split( "-" )[ 0 ]
    X=mktid(dato=dato, tid=tid )
    dato = y.split( "," )[ 0 ]
    tid  = y.split( "," )[ 1 ].split( "-" )[ 0 ]
    Y=mktid(dato=dato, tid=tid )

    return cmp( X, Y ) 

if __name__ == "__main__":
    Page = """Content-type: text/html

<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<b>
<a href="http://www.4guysfromrolla.com/webtech/082201-1.shtml">Howto run python on ASP</a>
<br>
<a href="http://support.microsoft.com/kb/276494">Using Python Scripts with IIS</a>
</b>
<pre>
<a href="http://www.eksperten.dk/spm/798330">Svar på http://www.eksperten.dk/spm/798330</a>
<h2>Før sortering</h2>
"""

    print Page
    print Liste

    liste=[ line[1:] for line in Liste.split("\n") ]
    liste.sort( cmp=sort_dato )

    print "<h2>Resultat af sortering</h2>;%s" % "\n;".join( liste )
    print "<hr><h2>KildeTekst</h2>"
    print open("/srv/www/cgi-bin/pysort.py").read()
    print "</pre></html>"
Relateret indhold
« august 2018 »
søn man tir ons tor fre lør
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 31
Chat Rooms
Dyrk motion

Trim ballerup

 

Powered by Plone, the Open Source Content Management System