Seite 1 von 1

programme zicken herum

Verfasst: Sonntag 20. Mai 2007, 09:28
von irgendwer
ich ärger mich mit unauffindbaten fehlern herum
warum bekomme ich bei anzgif++ die meldung invalid syntax

Code: Alles auswählen

#!/usr/bin/python
try:
	f = file('test-access-log.txt', 'r') 
	anzgif=0
	while True:
		line = f.readline()
		if len(line) == 0:
			break
		if line.find(gif,0,-1)!= -1:
			anzgif++
	f.close() 
	print "Gif Aufrufe:"+`AnzGif` +"\n"
except IOError:
dann hab ich noch ein 2tes prog dass im idle funtz aber im cgi nicht:

Code: Alles auswählen

#!/usr/bin/env python
print 'Content-Type: text/html\n\n' 
print''
import cgi
import cgitb
cgitb.enable()

print '<html>\n'
import time

hour, minute, second = time.localtime()[3:6]
wert= hour*minute*second*190
schrift=wert^2147483647
hexwert =hex(wert)
hexschrift =hex(schrift)
print'''<head> <title>Dynamische Hintergrundfarbe</title> </head>
<body bgcolor=\"''' +hexwert[2:]+'''\" text="'''+hexschrift[2:]+'''
><h1>'''+ time.ctime()+'''</h1></body></html>'''

Content-Type: text/html --> --> -->

Traceback (most recent call last):
File "time.py", line 4, in ?
import cgi
File "/usr/lib/python2.3/cgi.py", line 39, in ?
import urllib
File "/usr/lib/python2.3/urllib.py", line 28, in ?
import time
File "/home/stud2/rkammers/public_html/cgi-bin/time.py", line 15, in ?
hour, minute, second = time.localtime()[3:6]
AttributeError: 'module' object has no attribute 'localtime'

Re: programme zicken herum

Verfasst: Sonntag 20. Mai 2007, 09:46
von birkenfeld
irgendwer hat geschrieben:ich ärger mich mit unauffindbaten fehlern herum
warum bekomme ich bei anzgif++ die meldung invalid syntax
Es könnte daran liegen, dass es den "++"-Operator in Python nicht gibt. ;)
dann hab ich noch ein 2tes prog dass im idle funtz aber im cgi nicht:
Content-Type: text/html --> --> -->

Traceback (most recent call last):
File "time.py", line 4, in ?
import cgi
File "/usr/lib/python2.3/cgi.py", line 39, in ?
import urllib
File "/usr/lib/python2.3/urllib.py", line 28, in ?
import time
File "/home/stud2/rkammers/public_html/cgi-bin/time.py", line 15, in ?
hour, minute, second = time.localtime()[3:6]
AttributeError: 'module' object has no attribute 'localtime'
Du musst dein "time.py" umbenennen. "import time" importiert sonst das und nicht das Standardbibliotheks-time.

Re: programme zicken herum

Verfasst: Sonntag 20. Mai 2007, 12:07
von Joghurt
birkenfeld hat geschrieben:Es könnte daran liegen, dass es den "++"-Operator in Python nicht gibt. ;)
Den += Operator gibt es aber. Deshalb einfach

Code: Alles auswählen

anzgif += 1
schreiben

Verfasst: Sonntag 20. Mai 2007, 15:29
von BlackJack
Die Schleife lässt sich auch etwas kürzer schreiben wenn man direkt über das Dateiobjekt itertiert und den ``in``-Operator anstelle von `find()` benutzt:

Code: Alles auswählen

    lines = open('test-access-log.txt', 'r')
    anzahl_gif = 0
    for line in lines:
        if gif in line:
            anzahl_gif += 1
    lines.close()
    print 'Gif-Aufrufe: %d\n' % anzahl_gif
Noch etwas kompakter geht es mit der `sum()`-Funktion und einem Generator-Ausdruck:

Code: Alles auswählen

    lines = open('test-access-log.txt', 'r')
    anzahl_gif = sum(1 for line in lines if gif in line)
    lines.close()
    print 'Gif-Aufrufe: %d\n' % anzahl_gif