473,573 Members | 3,145 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.
Jun 27 '08 #1
16 924
On May 13, 5:28*am, dannywebs...@go oglemail.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).

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.
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.
Jun 27 '08 #2
On May 13, 11:28 am, dannywebs...@go oglemail.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).

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.
I did indeed mean "os.popen", no "os.open"

Jun 27 '08 #3
On May 13, 5:46*am, dannywebs...@go oglemail.com wrote:
On May 13, 11:28 am, dannywebs...@go oglemail.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).
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.

I did indeed mean "os.popen", no "os.open"- Hide quoted text -

- Show quoted text -
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?
Jun 27 '08 #4
On May 13, 11:51 am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
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
Jun 27 '08 #5
En Tue, 13 May 2008 07:46:45 -0300, <da**********@g ooglemail.comes cribió:
On May 13, 11:28 am, dannywebs...@go oglemail.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

Jun 27 '08 #6
On May 13, 6:18*am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
En Tue, 13 May 2008 07:46:45 -0300, <dannywebs...@g ooglemail.comes cribió:
On May 13, 11:28 am, dannywebs...@go oglemail.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
Writing's fine, but don't the musicals suck?
Jun 27 '08 #7
On May 13, 6:18*am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
En Tue, 13 May 2008 07:46:45 -0300, <dannywebs...@g ooglemail.comes cribió:
On May 13, 11:28 am, dannywebs...@go oglemail.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
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!
Jun 27 '08 #8
da**********@go oglemail.com schrieb:
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
untested:
f=" ".join(f)
f.split()

Best regards

Wolfgang
Jun 27 '08 #9
On Tue, 2008-05-13 at 03:28 -0700, da**********@go oglemail.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).

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']"]
Instead of casting to a string, each element of your list is already a
string, so use that instead:

f = open("get_hostn ames")
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(' ')
Any help is highly appreciated

ta

dan.
--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2458
by: Don | last post by:
Using PHP, I'm creating an <input> tag. How do you delimit the text string for the value parm of an <input> tag, when the string contains the delimit char? Example: <input...value="This text has a " in it"...> I tried preceeding the " with a the escape char \, but the browser apparently still used the embedded " as termination of the...
5
2539
by: Daniel Pryde | last post by:
Hi everyone. I was wondering if anyone might be able to help me out here. I'm currently looking to find the quickest way to find a best fit match in a large array. My problem is that I have an array of, say, 600*400, which contains a value at each point, and I need to find the value in that array which is closest to the input value. It's...
14
3119
by: 42 | last post by:
Hi, Stupid question: I keep bumping into the desire to create classes and properties with the same name and the current favored naming conventions aren't automatically differentiating them... (both are "Pascal Case" with no leading or trailing qualifiers). For example... I'll be modelling something, e.g. a computer, and I'll
1
2466
by: msnews.microsoft.com | last post by:
I'd like to hear your thoughts on best methods for populating drop down list controls. I have states and countries drop down lists that don't change often, so naturally I "hard code" them in the aspx page. But the problem is these tend to really slow the development -- it takes up to 15 seconds for the page to come up in VS.NET design...
15
25787
by: Andrew Brampton | last post by:
Hi, This may sound a odd question, but I wanted to know how you return a list of data from a function. These are some of the ways I know how, and I was wondering which method you normally use. This is more of a best practices question rather than a technical one. 1) Return a list instance ie std::list myFunction() { std::list list();...
4
1751
by: Guy Noir | last post by:
Hello. Is there a pattern or best practice for the following scenario? I have a list of items I would like to compare. The number of items are decided at runtime. ObjectA, ObjectB, ObjectC......ObjectX I want to process a comparison for each of these projects. So, for
71
33163
by: David T. Ashley | last post by:
Where is the best place to define TRUE and FALSE? Are they in any of the standard include files, ever? Do any standards apply? What I've traditionally done is something like: #ifndef (TRUE) #define TRUE (1)
9
7870
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private List<as a property. Consider the example below, the class "ListTest" contains a private "List<>" called "strings" - it also provides a public...
5
12763
by: darthghandi | last post by:
I've created a class to listen to all interfaces and do a BeginAccept(). Once it gets a connection, it passes the connected socket off and stores it in a List. Next, it continues to listen for more incoming connections and does the BeginAccpet() again. It does an infinite loop this way. My question is: What is the best way to stop this? ...
0
7777
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6411
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5289
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3730
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3731
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2197
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1297
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1038
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.