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

Danathar dougb at attglobal.net
Thu Aug 22 21:08:07 CEST 2002


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.
> 
> -Dave
> 
> (hmm...that's not a very satisfying answer, huh?)


Thanks for all of the answers, after some more digging I found

os.getlogin - its seems to be specific to Python 2.2...

---

One other question.....

I have a file that has individual lines with comma seperated fields in
them. It was easy to load the entire file into a LIST by using
input.readlines(). So now I have a LIST with one line as each element
in the variable....but....

The goal is to scan each line of the file for inconsistancies in the
comma seperated fields...bad characters, incorrect number length and
so on. So I want to step through each field looking for bad data....

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....

example...

How do I turn List variable called "test"

test = 'john, bill, 1234, 4.5' , 'mary, bob, 4567, 9.2'

into

test = '['john', 'bill', 1234, 4.5], ['mary', 'bob', 4567, 9.2]'

I could sove the problem with a while or do conditional and step
through each of the original elemnts, look for commas, split them and
then re-assign them, but that seems like alot of work, is there a
better way?

Apologies for any ignorance on my part, I am determined to learn
Python and its new!

thanks



More information about the Python-list mailing list