Google Groups Home
Help | Sign in
Best way to delimit a list?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  18 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
dannywebs...@googlemail.com  
View profile
 More options May 13, 6:28 am
Newsgroups: comp.lang.python
From: dannywebs...@googlemail.com
Date: Tue, 13 May 2008 03:28:03 -0700 (PDT)
Local: Tues, May 13 2008 6:28 am
Subject: Best way to delimit a list?
Hi - I have a list returned from popen/readlines, and am wondering how
to go about iterating over each item which was returned (rather than
currently having the whole lot returned).

so far:

>>> f=os.open("./get_hostnames").readlines

returns ['host1 host2 host3 ... hostN\n]'

i'd like to be in a position to iterate through these, grabbing each
host.  I have played with transmuting to a str, and using split, and
this works, but I get the subscript brackets from the list output as
expected, as the list output is now a string literal, and this is not
what I want - and I think it's a bit long-winded to do a search 'n
replace on it - hence why I ask in the subject what's the best way.

>>> f=str(f)
>>> f.split()

["['host1","host2", ... ,"hostN\n']"]

Any help is highly appreciated

ta

dan.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
castiro...@gmail.com  
View profile
(1 user)  More options May 13, 6:45 am
Newsgroups: comp.lang.python
From: castiro...@gmail.com
Date: Tue, 13 May 2008 03:45:17 -0700 (PDT)
Local: Tues, May 13 2008 6:45 am
Subject: Re: Best way to delimit a list?
On May 13, 5:28 am, dannywebs...@googlemail.com wrote:

Bring up the Google Ring.  Where you only wiggle fingers, it might pay
to get jobs at home.  All we up here would have to do would be
schedule something.  Make a decision is easy in talking.  I think it
would be easy to centralize the time the world's at and redistribute
money.  If all we'd do is normal life, this constrained, open markets
would be easy to set up.  It's just illegal to talk about pricing in
2004 Microecon. classes.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
dannywebs...@googlemail.com  
View profile
 More options May 13, 6:46 am
Newsgroups: comp.lang.python
From: dannywebs...@googlemail.com
Date: Tue, 13 May 2008 03:46:45 -0700 (PDT)
Local: Tues, May 13 2008 6:46 am
Subject: Re: Best way to delimit a list?
On May 13, 11:28 am, dannywebs...@googlemail.com wrote:

I did indeed mean "os.popen", no "os.open"

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gabriel Genellina  
View profile
 More options May 13, 6:51 am
Newsgroups: comp.lang.python
From: "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
Date: Tue, 13 May 2008 07:51:06 -0300
Local: Tues, May 13 2008 6:51 am
Subject: Re: Best way to delimit a list?
En Tue, 13 May 2008 07:28:03 -0300, <dannywebs...@googlemail.com> escribió:

> Hi - I have a list returned from popen/readlines, and am wondering how
> to go about iterating over each item which was returned (rather than
> currently having the whole lot returned).

> so far:

>>>> f=os.open("./get_hostnames").readlines

> returns ['host1 host2 host3 ... hostN\n]'

You meant readlines(), I presume. A file acts as its own iterator:

f=os.open("./get_hostnames")
try:
   for line in f:
     # do something with line
finally:
   f.close()

--
Gabriel Genellina


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
castiro...@gmail.com  
View profile
(1 user)  More options May 13, 6:57 am
Newsgroups: comp.lang.python
From: castiro...@gmail.com
Date: Tue, 13 May 2008 03:57:35 -0700 (PDT)
Local: Tues, May 13 2008 6:57 am
Subject: Re: Best way to delimit a list?
On May 13, 5:46 am, dannywebs...@googlemail.com wrote:

I do indeed write a pretty fine real-time, low-bandwidth, game.  It is
like real-time chess, and seen the movie, Tron.  Can't the P2Ps zip up
in an hour?

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
dannywebs...@googlemail.com  
View profile
 More options May 13, 7:14 am
Newsgroups: comp.lang.python
From: dannywebs...@googlemail.com
Date: Tue, 13 May 2008 04:14:16 -0700 (PDT)
Local: Tues, May 13 2008 7:14 am
Subject: Re: Best way to delimit a list?
On May 13, 11:51 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:

> You meant readlines(), I presume. A file acts as its own iterator:

> f=os.open("./get_hostnames")
> try:
>    for line in f:
>      # do something with line
> finally:
>    f.close()

> --
> Gabriel Genellina

Hi - thank you for your reply.

I meant:

f=os.popen("./get_hostnames").readlines()

So f is a list, rather than a file object, of which os.open would have
returned (my initial typo redirected the missive of this post, sorry!)

cheers


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gabriel Genellina  
View profile
 More options May 13, 7:18 am
Newsgroups: comp.lang.python
From: "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
Date: Tue, 13 May 2008 08:18:59 -0300
Local: Tues, May 13 2008 7:18 am
Subject: Re: Best way to delimit a list?
En Tue, 13 May 2008 07:46:45 -0300, <dannywebs...@googlemail.com> escribió:

> On May 13, 11:28 am, dannywebs...@googlemail.com wrote:
>> Hi - I have a list returned from popen/readlines, and am wondering how
>> to go about iterating over each item which was returned (rather than
>> currently having the whole lot returned).

>> >>> f=os.open("./get_hostnames").readlines

> I did indeed mean "os.popen", no "os.open"

Ouch, replace open with popen an my example is valid (but to get the  
meaning I intended to write, replace os.open with open...)

--
Gabriel Genellina


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
castiro...@gmail.com  
View profile
(1 user)  More options May 13, 7:20 am
Newsgroups: comp.lang.python
From: castiro...@gmail.com
Date: Tue, 13 May 2008 04:20:32 -0700 (PDT)
Local: Tues, May 13 2008 7:20 am
Subject: Re: Best way to delimit a list?
On May 13, 6:18 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:

Writing's fine, but don't the musicals suck?

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
castiro...@gmail.com  
View profile
(1 user)  More options May 13, 7:24 am
Newsgroups: comp.lang.python
From: castiro...@gmail.com
Date: Tue, 13 May 2008 04:24:00 -0700 (PDT)
Subject: Re: Best way to delimit a list?
On May 13, 6:18 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:

Yes: fine!  But, all we do is start a Tron ring, play Tron on
laptops.  You have micro-divide currency, you can probably make
musicals -too-; and I don't have enough to say to get this...

BE TALKING!


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Wolfgang Grafen  
View profile
 More options May 13, 7:44 am
Newsgroups: comp.lang.python
From: Wolfgang Grafen <wolfgang.gra...@ericsson.com>
Date: Tue, 13 May 2008 13:44:27 +0200
Local: Tues, May 13 2008 7:44 am
Subject: Re: Best way to delimit a list?
dannywebs...@googlemail.com schrieb:

untested:
f=" ".join(f)
f.split()

Best regards

Wolfgang


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
J. Clifford Dyer  
View profile
 More options May 13, 8:15 am
Newsgroups: comp.lang.python
From: "J. Clifford Dyer" <j...@sdf.lonestar.org>
Date: Tue, 13 May 2008 08:15:55 -0400
Local: Tues, May 13 2008 8:15 am
Subject: Re: Best way to delimit a list?

Instead of casting to a string, each element of your list is already a
string, so use that instead:

f = open("get_hostnames")
hosts =[]

# gets each string one at a time.
for line in f:
    # get rid of the pesky \n at the end
    line = line.strip()
    # separate the hostnames into a list
    hosts += line.split(' ')


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.