Re: Newbie question...



Ken D'Ambrosio wrote:
First, apologies for such a newbie question; if there's a better forum (I've poked around, some) feel free to point it out to me. Anyway, a mere 25-odd years after first hearing about OOP, I've finally decided to go to it, by way of Python. But this puzzles me:

import commands
free = commands.getoutput("free")
for line in free:
print line,

Gives:
t o t a l u s e d f r e e
s h a r e d b u f f e r s c a c h e d
M e m : 5 1 5 9 9 2 4 6 0 4 5 2 5 5 5 4 0
0 7 7 5 1 6 9 1 8 8 4
- / + b u f f e r s / c a c h e : 2 9 1 0 5 2 2 2 4 9 4 0

Why are there spaces between everything? And how do I keep it from happening? *confused*

Thanks much,

-Ken
** Posted from http://www.teranews.com **
--
http://mail.python.org/mailman/listinfo/python-list

The variable 'free' is a string containing all of the output, not a file object or a sequence of strings. Therefore, when you iterate free you iterate a sequence of characters. This is different than the case of iterating an open file, which would give you a sequence of lines as you expect.

So ...

print line,

.... prints each character followed by a space and no newline.

You can do this instead:

import commands
free = commands.getoutput("free")
print free


- Ken (that's my name too)

.



Relevant Pages

  • for-each on atomic sequence
    ... I construct a sequence using 'tokenize' on a space-separated string ... and then try to iterate over it with for-each, ... In other words there is only one element in the sequence which is ...
    (comp.text.xml)
  • Re: Can anyone explain a part of a telnet client code to me..
    ... The for statement is used to iterate over a sequence; a string is considered a sequence of characters, so it iterates over all the characters in the string, one by one. ...
    (comp.lang.python)
  • Merry Christmas!
    ... dictionaries and every variant possibility has a separate "word" entry. ... The byte string of the "word", whose length is specified by a four ... match is found for a source byte sequence in the dictionary. ...
    (rec.arts.sf.written)
  • Merry Christmas! Linux RULES! New applications to develop!
    ... dictionaries and every variant possibility has a separate "word" ... Each entry in the dictionary contains: ... The byte string of the "word", whose length is specified by a four ... addresses whose entry is selected by the first byte of the sequence. ...
    (comp.os.linux.misc)
  • Re: user defined function that converts string to float
    ... > I need user defined function that converts string to float in c. ... initial, possibly empty, sequence of white-space characters (as ... point character, then an optional exponent part as defined in ... then a nonempty sequence of hexadecimal digits ...
    (comp.lang.c)