A stupid newbie question about output...

Ethan Furman ethan at stoneleaf.us
Tue Oct 20 14:53:45 EDT 2009


J wrote:
> Can someone explain why this code results in two different outputs?
> 
> 
>>for os in comp.CIM_OperatingSystem ():
>> print os.Name.split("|")[0] + " Service Pack", os.ServicePackMajorVersion
>> osVer = os.Name.split("|")[0] + " Service Pack", os.ServicePackMajorVersion
>> print osVer
> 
> 
> the first print statement gives me this:
>   Microsoft Windows XP Professional Service Pack 3
> 
> and the second print statement gives me this:
> (u'Microsoft Windows XP Professional Service Pack', 3)
> 
> I know this is a grossly oversimplified example.
> 
> The ultimate goal is to get certain things from the system via WMI and
> then use that data in other scripts to generate test case scripts for
> a testing tool.
> 
> So given this example, what I ultimately want is to create a class
> full of this stuff, that I can call from another program like this:
> 
> 
>>import SystemInformation
> 
> 
>>comp = SystemInformation()
> 
> 
>># Get OS info
>>OSVersion = comp.osVer
> 
> 
> OR just print the result
> 
> 
>>print comp.osVer
> 
> 
> Can this be cleared up by using some sort of formatting in print (like
> sprintf) or am I going to have to manipulate the WMI results before
> sending them out??
> 
> Thanks
> 
> Jeff
> 

The issue is that on line one you are printing two strings, and on line 
three you are printing one tuple.  It's easier to show on Python 3:

Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit 
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

--> print('this','that')
this that

--> temp = 'this','that'
--> temp
('this', 'that')

--> print(temp)
('this', 'that')

--> print(*temp)  # pass the pieces of temp individually
this that

What you probably want on line two is:

osVer = os.Name.split("|")[0] + " Service Pack " + \
         str(os.ServicePackMajorVersion)

or, in Python 2.x:

osVer = "%s Service Pack %d" % (os.Name.split("|")[0],
         os.ServicePackMajorVersion)

This way, osVer is a string, and not a tuple.

Hope this helps.

~Ethan~



More information about the Python-list mailing list