creating a subnested lists from a comma seperated string read from a file

Donn Cave donn at u.washington.edu
Thu Aug 22 15:32:33 EDT 2002


Quoth dougb at attglobal.net (Danathar):
| brueckd at tbye.com wrote in message news:<mailman.1029952359.31127.python-list at python.org>...
|> On 21 Aug 2002, Danathar wrote:
|>| Those methods work, but I am trying to learn python, and somehow it
|>| feels like I am cheeting by using python to call a shell command (like
|>| whoami) that I already know. Getting the USER variable works, but what
|>| if you are not in a UNIX envornment where the USER variable does not
|>| exist?
|> 
|> Well, USERNAME is defined in Windows (at least some versions). ;-) 
|> 
|> Yeah, I know the feeling you have, but does having the user's ID on a
|> non-Unixish platform even mean anything? On Windows at least it's often
|> pretty worthless, and almost anything you can do with it is pretty
|> platform-specific anyway.

| Thanks for all of the answers, after some more digging I found
|
| os.getlogin - its seems to be specific to Python 2.2...

Whatever works for you is great, but os.getlogin() is the last thing
I would use.  Details of how it works vary from one platform to another
(which is the first strike against it), and on some of them it works
in obscure or arguably defective ways.  (Cf. AIX for obscure, some
versions of Linux for defective.)  It isn't a general solution for
"what user".

| The problem is that my LIST variable elements are strings of each
| entire line that was read with readlines(). I know that LISTS can have
| elements that are themselves lists called netsted sublists.
|
| How can I take each string element in my LIST variable with my comma
| seperated fields and turn them into a subnested lists with each comma
| seperated field as individual elements....

You can split a string with split(), like sublist = string.split(line, ',')
Or if Python >= 2.0, you can say sublist = line.split(',')

If the separator is more complicated, like comma plus 0 or more spaces,
then you might look up re.split.

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list