Seite 1 von 1

String aufsplitten nach Länge

Verfasst: Donnerstag 19. Februar 2009, 19:13
von hachel
Hallo,
Ich hab das folgende pythonscript das mir von der englischen Wikipedia den Abschnitt "on this day" ausliest.
Den zerteile ich dann in kleine happen und will die später via conky auf meinem Desktop anzeigen.
Als hier erstmal das Script: Ich muss dazu sagen das ich bis vor 20 Minuten noch nie python gescriptet hab. Ich also kein plan von Syntax oder eventuellen Vor- und Nachteilen gewisser Methoden und Bibliotheken.

Code: Alles auswählen

#!/usr/bin/python
import urllib2
import re
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
infile = opener.open('http://en.wikipedia.org/wiki/Main_Page')
page = infile.read()

max_length = 80

page_stripped = re.sub(r'<[^>]*?>', '', page)

start = page_stripped.find('On this day...');
end = page_stripped.find('More events');

otd_raw = page_stripped[start+14:end]

otd_list = otd_raw.splitlines()

for item in otd_list:
	if len(item) > 0:
		#if len(item) > max_length:
		#	item = item[0:max_length]
		print item
Und das kommt dabei raus:

$python Desktop/wikipedia.py
February 19: Fat Thursday (2009)
1594 – King Sigismund III Vasa of the Polish–Lithuanian Commonwealth was crowned King of Sweden, succeeding his father John III.
1600 – The Peruvian stratovolcano Huaynaputina exploded in the most violent eruption in the recorded history of South America.
1811 – Peninsular War: An outnumbered French force under Édouard Mortier (pictured) routed and nearly destroyed the Spanish at the Battle of the Gebora near Badajoz, Spain.
1819 – English explorer William Smith sighted Livingston Island in the South Shetland archipelago, a group of Antarctic islands more than 1,000 kilometres (620 mi) south of the Falkland Islands.
1942 – World War II: In the largest attacks mounted by a foreign power against Australia, more than 240 bombers and fighters of the Imperial Japanese Navy bombed Darwin, Northern Territory.
1986 – The space station Mir of the Soviet space program was launched, establishing the first long-term research station in space.

Das Problem ist jetzt das diese einzelnen Linien in ihrer Rohform viel zu lang sind, die sprengen meinen Desktop.
Ich hab daher diese (oben auskommentierte) Variante, dass ich den String einfach nach x Zeichen kappe. Das ist natürlich nicht optimal.
Ich hätte es halt gerne, dass der abgekappte Teil in der reihe darunter erscheint, nur weiß ich nicht wie ich das mache.

Kann mir da jemand helfen?
Falls es ne ganz andere, bessere Herangehensweise gibt freu ich mich auch die zu höhren.
Danke schonmal

PS: ich hatte überlegt noch mal zu splitten von max_length bis Ende, aber dieser 2. Teil könnte ja auch wieder über max_length Zeichen sein

PS2: falls jemand weiß ob sich das mit conky formatieren lässe, das wär natürlich am besten

Verfasst: Donnerstag 19. Februar 2009, 19:31
von numerix
Vielleicht hilft dir textwrap: http://docs.python.org/library/textwrap.html

Verfasst: Donnerstag 19. Februar 2009, 19:38
von Hyperion
HTML sollte man nicht unbedingt per RegExp parsen. Dazu gibt es Libs wie BeutifullSoap, html5lib, lxml usw.

Solche Anfragen haben wir hier oft; nen paar Threads weiter unten muss noch ein aktueller sein. Ansonsten nutze mal die SuFu.

Verfasst: Donnerstag 19. Februar 2009, 20:55
von hachel
danke, textwrap ist top

benutze jetzt beautifulsoap

danke

Verfasst: Freitag 20. Februar 2009, 21:39
von CM
Nachtrag: Für gerade erst mit Python angefangen, liest sich Dein Skript aber wirklich super!

Willkommen im Forum,
Christian

Verfasst: Samstag 21. Februar 2009, 20:36
von nkoehring
Da muss ich CM absolut beipflichten!