Unable to lookup keys in dictionary

Andreas Tawn andreas.tawn at ubisoft.com
Tue Aug 12 12:15:51 EDT 2008


>I am trying to run p4python API on top of python 2.5.2 and am
extracting a dictionary from perforce. The following code returns the
output that follows it:
>
>		from P4 import P4, P4Exception
>		p4 = P4()
>		p4.port = "erased"   #deleted
>		p4.user = "erased"   #deleted
>		 
>		try:
>		   p4.connect()
>		   info = p4.run("info")
>		   print info
>		   p4.disconnect()
>		except P4Exception:
>		   for e in p4.errors:
>		     print e
>		 
>
>	Output:
>
>		 
>		[{'userName': 'mfielding', 'clientRoot': 'c:\\perforce',
'monitor': 'enabled', 
>		'serverRoot': 'H:\\p4root\\', 'serverVersion':
'P4D/NTX64/2007.3/143793 (2008/01/
>		21)', 'serverDate': '2008/08/12 11:18:56 -0400 Eastern
Daylight Time', 'clientAd
>		dress': '10.24.20.97:1918', 'serverLicense': 'Mad Doc
Software, Inc. 140 users (
>		support expired 2008/05/31) ', 'serverAddress':
'rsgnwep4s1.rockstar.t2.corp:166
>		6', 'clientHost': 'nwew-mfielding', 'security':
'enabled', 'password': 'enabled'
>		, 'clientName': 'mfielding'}]
>
>	I did this to test that I was receiving a dictionary back from
the server, which I clearly am. I then followed by adding the following
lines to the code:
>	 
>
>		print info
>		s = info[serverVersion]
>		print s
>		p4.disconnect()
>
>	I would expect this to print out P4D/NTX64/2007.3/143793
(2008/01/23), but instead it spits out the following error:
>	 
>
>		Traceback (most recent call last):
>		  File "test.py", line 10, in <module>
>		    s = info[serverVersion]
>		NameError: name 'serverVersion' is not defined
>		 
>
>	Changing "s = info[serverVersion]" to "s =
info['serverVersion']" only gives me another error, which is:
>	 
>
>		Traceback (most recent call last):
>		  File "test.py", line 10, in <module>
>		    s = info['serverVersion']
>		TypeError: list indices must be integers
>
>	 
>	If anyone has any idea what is going on here, I would appreciate
the help. I've spent a few hours over the past two days trying to figure
this little quirk out, but to no avail.

It looks like you're getting a list with the dictionary as the only
element.

Try...

s = info[0]['serverVersion']

Cheers,

Drea



More information about the Python-list mailing list