Search code examples
pythonlistfile-iocherrypy

In python, how to break list of strings into list of list of strings?


I am running a server with cherrypy and python script. Currently, there is a web page containing data of a list of people, which i need to get. The format of the web page is as follow:

www.url1.com, firstName_1, lastName_1
www.url2.com, firstName_2, lastName_2
www.url3.com, firstName_3, lastName_3 

I wish to display the list of names on my own webpage, with each name hyperlinked to their corresponding website.

I have read the webpage into a list with the following method:

@cherrypy.expose
    def receiveData(self):
        """ Get a list, one per line, of currently known online addresses,
            separated by commas.
        """

        method = "whoonline"
        fptr = urllib2.urlopen("%s/%s" % (masterServer, method))
        data = fptr.readlines()
        fptr.close()

        return data

But I don't know how to break the list into a list of lists at where the comma are. The result should give each smaller list three elements; URL, First Name, and Last Name. So I was wondering if anyone could help.

Thank you in advance!


Solution

  • You can iterate over fptr, no need to call readlines()

    data = [line.split(', ') for line in fptr]