Reputation: 2015
I have a dict where some of the values are lists. Unfortunately, Python thinks they are strings. The dict looks like this:
dict = {'key': 'x', 'key1': '[1, 2, 3], ...'}
When I pull dict['key1']
and try to perform list operations on it, I get tracebacks that say I can't because the element is a string. No problem, but converting it from a string to a list converts each character of the string separately.
list = list(dict['key1'])
>>> ['[', '1', ',' ' ', '2', ',', ' ', '3', ']']
how do I convert the string to get this as a list type?
>>> [1, 2, 3]
Python 2.5 is a requirement.
Updated: corrected to make the list a string. Update 2: includes code used to generate the dict. It creates a dict from an XML source.
class XmlDictConfig(dict):
def __init__(self, parent_element):
if parent_element.items():
self.updateShim(dict(parent_element.items()))
for element in parent_element:
if len(element):
aDict = XmlDictConfig(element)
if element.items():
aDict.updateShim(dict(element.items()))
self.updateShim({element.tag: aDict})
elif element.items():
self.updateShim({element.tag: dict(element.items())})
else:
self.updateShim({element.tag: element.text}) # WAS: _self.updateShim({element.tag: element.text.strip()})_ with strip(), the function will choke on some XML.
def updateShim (self, aDict ):
for key in aDict.keys():
if key in self:
value = self.pop(key)
if type(value) is not list:
listOfDicts = []
listOfDicts.append(value)
listOfDicts.append(aDict[key])
self.update({key: listOfDicts})
else:
value.append(aDict[key])
self.update({key: value})
else:
self.update(aDict)
def flatten_dict(d):
def expand(key, value):
if isinstance(value, dict):
return [ (key + '_' + k, v) for k, v in flatten_dict(value).items() ]
else:
return [ (key, value) ]
items = [ item for k, v in d.items() for item in expand(k, v) ]
return dict(items)
try:
socket.setdefaulttimeout(15)
f = urllib2.urlopen(xmlAddress)
data = str(f.read())
f.close()
xmlRawData = re.sub(' xmlns="[^"]+"', '', data, count=1)
root = ElementTree.XML(xmlRawData)
except:
print "Something went very wrong."
xmlDict = XmlDictConfig(root)
flatxmlDict = flatten_dict(xmlDict)
finalDict = {}
for (key, value) in flatxmlDict.items():
finalDict[key] = str(value)
Upvotes: 1
Views: 182
Reputation: 8326
You can use the ast
library to convert strings of code into actual python code.
ast.literal_eval(''.join(list(dict['key1'])))
e.g.
>>> import ast
>>> _list = ['[', '1', ',' ' ', '2', ',', ' ', '3', ']']
>>> ast.literal_eval(''.join(_list))
[1, 2, 3]
Update
As ast.literal_eval
is 2.6 and onward, see this post about backporting literal_eval
to 2.5
Upvotes: 0
Reputation:
If you mean you have a dictionary like this:
dct = {'key': 'x', 'key1': '[1, 2, 3]'}
you can use ast.literal_eval
to make make the string representation of a list into an actual list:
>>> from ast import literal_eval
>>> dct = {'key': 'x', 'key1': '[1, 2, 3]'}
>>> dct['key1'] = literal_eval(dct['key1'])
>>> dct
{'key': 'x', 'key1': [1, 2, 3]}
>>> type(dct['key1'])
<class 'list'>
>>>
Of course, it would probably be better to investigate why the list is being made into a string in the first place and then fix that instead.
Upvotes: 2