Processing on a string

Fredrik Lundh fredrik at effbot.org
Mon Feb 5 16:30:48 EST 2001


Jay Collins wrote:
> I have a list that is all the lines from the password file.
>
> if I did print p[0] # p being a line in /etc/passwd
> I would get:
>
> uucp:x:10:14:uucp:/var/spool/uucp:

how about:

    import pwd
    mylist = [entry[0] for entry in pwd.getpwall()]

> or whatever. My question is how do process this list into another
> list with just everything before the first ":". I'm not quite sure how to
> do this.

...but if you insist on reading /etc/passwd yourself,
you could do:

    passwd = open("/etc/passwd").readlines()
    mylist = [entry.split(":", 1)[0] for entry in passwd]

hope this helps!

Cheers /F

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->





More information about the Python-list mailing list