how to list elements line by line?
magni2k
magni2k at uni.de
Fri May 17 07:57:50 EDT 2002
Hi,
I have a problem. How can I list all the elements in my serverlist.txt
seperate. That means line by line! So that I can call for example: List only
the Hostnames! And on the other hand that I can call for example: List only
the 1st Hostname, then the 2nd and so on! (If its possible with the sys.argv
argument!)
Anybody an idea how to handle it? Is it necessary to create a new class or
does it with methods? I would be very appreciate for any help or hints.
I have listed the source code and the .txt file. Thanks.
magni
filelist.py
import sys, urlparse
class ListFile:
def __init__(self, filename):
self.__file = open(filename, 'r')
def __getitem__(self):
line = self.__file.readlines()
if line:
return line
else:
raise IndexError
def __getattr__(self, name):
return getattr(self.__file, name)
def PrintLine(self):
for line in self.__file:
print line,
return line
class URL:
def __init__(self, url):
self.__url = self.SetURL(url)
def Analyze(self):
parse = urlparse.urlparse(self.__url)
self.__protocol = parse[0]
self.__hostname = parse[1]
self.__path = parse[2]
def GetProtocol(self):
return self.__protocol
def GetHostname(self):
return self.__hostname
def GetPath(self):
return self.__path
def GetAll(self):
return self.__url
def SetProtocol(self, protocol):
self.__protocol = protocol
return self.__protocol
def SetHostname(self, hostname):
self.__hostname = hostname
return self.__hostname
def SetPath(self, path):
self.__path = path
return self.__path
def SetURL(self, url):
self.__seturl = url
return self.__seturl
if __name__ == '__main__':
#u = URL('http://www.python.de/test')
#u.Analyze()
#print u.GetProtocol()
#print u.GetHostname()
#print u.SetPath('/index.html')
#print u.SetProtocol('ftp')
#file = ListFile(sys.argv[1])
#file.PrintLine()
for line in ListFile(sys.argv[1]):
u = URL(line)
u.Analyze()
print u.GetHostname()
serverlist.txt contains for e.g.:
http://www.python.org/index.html
ftp://starship.python.org/python/distr
http://www.python.de/start.html
---------------------------------------------------------------------
Dir wird das Leben zu teuer? Dann zahl doch einfach weniger!
Hammer-Schnäppchen von raba.tt exklusiv bei FirstCampus.de
---------------------------------------------------------------------
More information about the Python-list
mailing list