1

Possible Duplicate:
Creating or assigning variables from a dictionary in Python

Hello

we have dictionary like this:

data = {'raw':'lores ipsum', 'code': 500}

how to convert this dict to strings? result must be like this:

print raw
print code
#output is 
#  lores ipsum
#  500

EDIT: OK, so what I want is to have raw and code variable available.. one more example

data = {'raw':'lores ipsum', 'code': 500}
var1 = code
var2 = raw

it's becomes difficult to make tons of string operations with variable, which is dict, which is in class method... so it's endups with this: self.data['raw'][0] , it's killing me to write this everytime so to make "raw" variable is more comfortable... (imagine join and format operation with using this on every line!!!) anyway this vars will be available only in this method, so there's no side-effects for this solution...

EDIT: deleted, nobody understands what I want

Community
  • 1
  • 1
holms
  • 9,112
  • 14
  • 65
  • 95

4 Answers4

7

This is a pretty bad idea - using dicts as intended ends up with a much less confusing solution.

A quick hack dirty way of making the key=value pairs into non-dict string variables is as follows:

for key in data:
  exec("{0} = '{1}'".format(key,data[key]))

This is fragile and breaks with complex data types, aside from being ill-advised, but it does what you've asked.

Billy Herren
  • 671
  • 5
  • 4
3

Python Tutorial, §5.5, "Dictionaries"

print data['raw']
print data['code']
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
3

Very unclear to me what you exactly want, if you just want to print out the value of some dict key:

>>> print data['raw'] # etc
lores ipsum

will do the job.

If you want to print out all the keys, simple iterate over them:

for k, v in data.iteritems():
    print v

# ouput
# lores ipsum
# 500

But keep in mind that the dict is unordered, that means, the order of the output is not necessarily the order in which you declared the items.

Ivo Wetzel
  • 46,459
  • 16
  • 98
  • 112
  • edited question, please edit your post ;) – holms Dec 05 '10 at 06:17
  • That's no doable, at least not without a serious amount of witchcraft being involved, but even then it's not desirable, pulling the stuff out of the dict like this may override local variables and other stuff. – Ivo Wetzel Dec 05 '10 at 06:23
  • so it's better to do this manually? code = self.data['raw']['code'] ? you know this is just killing me... in php it's possible to convert array to variable with one function... and you'll have all indexes as var names... it's really comfortable.. in python it's a suicide... – holms Dec 05 '10 at 07:21
  • You could still do things like `raw = self.data['raw']; raw['code']` to lessen the amount you have to type. – Ivo Wetzel Dec 05 '10 at 07:50
1

Python local and globals variable are just kept in dicts as well. You just have to take one of these dicts, through the locals() or globals() function and update it.

It works, be warned it is not safe if you are getting this data from an external source, as any name on your environment (including those of built in functions) can be overriden this way.

>>> data = {'raw':'lores ipsum', 'code': 500}
>>> locals().update(data)
>>> print raw
lores ipsum
>>> print code
500
>>>
jsbueno
  • 99,910
  • 10
  • 151
  • 209