473,802 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.getout put("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 1004
On Tue, Sep 30, 2008 at 12:04 PM, Ken D'Ambrosio <ke*@jots.orgwr ote:
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.getout put("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.getout put("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.getout put("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
1578
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
1069
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 lngCount = 0 To lstIncluded.Items.Count - 1 206: If lstIncluded.GetSelected(lngCount) Then 207: strMembers = strMembers & VB6.GetItemData(lstIncluded, lngCount) & "," End If 209: Next 211: If Len(strMembers) > 0 Then 212: strMembers =...
5
2522
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( ...) { in >> value do stuff with value; }
5
1151
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 newbie. So, unable to take a call as to how-important or desirable the newer language features are -- so whether to write my app for v2.5 of Python, versus, as few others on this list have recommended, i.e. to stick to v2.3 ?? Are the preformance...
16
1885
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 in c++/java !? Tnx, Raxit
7
1518
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 value per pixel, and set it to something else if it meets or falls below a certain threshold - i.e., a Red value of 0 would be changed to 50. I've built my list by using a Python Image Library statement akin to the following:
5
2706
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 tag). Why doesn't the first alert statement pick up the style.left attribute from the CSS? <html> <head> <style type="text/css">
12
1858
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 j, v = a;
3
2346
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 Oxygen, to HTML kit, et cetera. I am more confused than ever. here's my quandry. I was sent a file and told that we need to get the data into a more readable form. I've tried using apps to pull it in, but it's not displaying properly.
5
1400
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 get a long skinny button that extends to the right side of the browser. This does not appear to happen with other controls. Any idea why it happens to the button? Here is how the button looks in source code:
0
10538
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10305
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10285
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6838
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5494
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
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
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.