Vlad Vorobiev <schrot=Hlh/39ujc8LR7s880joybQ@public.gmane.org> wrote:
Hallo, Ich bin gerade zu euch getretten.
Ich heisse Vlad Vorobiev und wohne in Koeln, mit Python beschaftige ich mich seit ca. 3 Monaten, also weiss ich noch praktisch gar nix. Das war meine Vorstellung, ich weiss nicht was ich noch erzahlen soll also Fragt falls euch was interesiert.
Jetzt aber zu meiner Frage:
import urllib def site(): site = urllib.urlopen('http://xxx') READ=site.read()
Damit lese ich den Quellcode einer HTML seite aus. Wie kann ich das Auslesen begrenzen. Z.B sagen das er nur von der Zeile 5 bis Zeile 8 lesen soll?
#v+ Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.
url = "http://python.org" import urllib f = urllib.urlopen(url) f.read(10) '<!DOCTYPE ' f.read(10) 'html PUBLI' f.readline() 'C "-//W3C//DTD HTML 4.01 Transitional//EN">\n' f.readline() '<?xml-stylesheet href="./css/ht2html.css" type="text/css"?>\n' f.readline() '<html>\n' #v-
Wie du sehen kannst, hat read() einen optionalen Parameter, der die Anzahl der zu lesenden Bytes angbit. Mit readline() liest du zeilenweise. Und readlines() liest das ganze "File" in eine Liste. Die Zeilen 5 bis 8 könntest du z. B. mit: f.readlines()[4:7] lesen. Ist zwar nicht die effektivste Methode ...
Und direct mal eine zweite Frage: string.replace(str(a),'xxx','yyy ')
Dieses ersetzt xxx durch yyy. Funktioniert nur Zeilenweise.
Stimmt nicht. string.replace ersetzt alle vorkommenden Strings 'old' durch 'new' bei string.replace(s, old, new). Zeilenumbrüche interessieren string.replace herzlich wenig ;-)
Bei folgendem Text geht es schon nicht mehr: xxxxxx cccccccccc cccccccccccccccccc
string.replace(str(a),'xxxxxx cccccccccc cccccccccccccccccc,'yyy ') !!! :) mein ansatz.
Diesen Absatz kapier ich nicht - die cccc... sind jedenfalls kein gültiges Python. Kannst du mal deinen *tatsächlichen* Code posten, damit man sehen kann, was da passiert? -- Gerhard _______________________________________________ Python-de maillist - Python-de@starship.python.net http://starship.python.net/mailman/listinfo/python-de