473,396 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Newbie question...

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 **
Sep 30 '08 #1
2 986
On Tue, Sep 30, 2008 at 12:04 PM, Ken D'Ambrosio <ke*@jots.orgwrote:
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")
# free is now a string, representing the output from the "free" command

for line in free:
print line,

This isn't doing what you think. Since free is a string, when you
iterate over it, you get a single character each time, so your line
variable isn't actually a line of the output, but a single character.
When you run "print line," this prints the character, followed by a
space. The comma at the end of the print statement tells it to put a
space after the output rather than a newline.

What you probably wanted to do is split up your output on the newline character.

for line in free.split("\n"):
print line

Of course, your string already has all the newlines it needs in it, so
if all you want is to see the output of the "free" command you can
just do:

print free

HTH,

Tim
>
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
Sep 30 '08 #2
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)

Sep 30 '08 #3

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

Similar topics

0
by: Elger | last post by:
Dear Members, A newbie question How do I convert this XML example into HTML (using XSLT)? <DOCUMENT> <PARA> This <BOLD>is</BOLD> a <BOLD>test</BOLD> </PARA> </DOCUMENT>
4
by: DragonLord | last post by:
I have a custom user control that i am trying to understand and it has numbers before the line items. So here it comes the real newbie question What the heck are the numbers for?? 205: For...
5
by: kamikaze04 | last post by:
Hello. I have a very newbie question about Streams. The situation is that i have a function (that i cannot modify it's definition/call): public void F1(istream & in){ while( ...) { ...
5
by: Banibrata Dutta | last post by:
Hi, I've gone through the list of "language differences" between 2.3 / 2.4 & 2.5 of CPython. I've spend around 2 weeks now, learning v2.5 of CPython, and I consider myself still very very...
16
by: Raxit | last post by:
Hi, i was reading/learning some hello world program in python. I think its very simillar to Java/C++/C#. What's different (except syntax) ? what can i do easily with python which is not easy...
7
by: idiolect | last post by:
Hi all - Sorry to plague you with another newbie question from a lurker. Hopefully, this will be simple. I have a list full of RGB pixel values read from an image. I want to test each RGB band...
5
by: Randall | last post by:
I am a newbie trying to learn the DOM. Can someone tell me why the first alert statement returns null, and the second returns the value 33px (which was set using the style="top:33px;" in the DIV...
12
by: Philipp.Weissenbacher | last post by:
Hi all! This is most certainly a total newbie question, but why doesn't the following code cause a segfault? void insertion_sort(int a, int length) { int i; for (i=0; i < length; i++) { int...
3
Lokean
by: Lokean | last post by:
Sorry for this newbie question, this is not my realm of expertese. I have searched google, tried several applications that claim they can do this, such as Mapforce, which I found confusing, to...
5
by: Dave | last post by:
I am new to Visual Web Developer 2005 Expres. I am using absolute positioning and every time I add a button control to my web form its width extends all the way to the edge of the page. IOW I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.