I have an excel document with a row, something like:
{MGY: {1: 85, 2: 15}}
{MGY: {1:85, 2:15}, MWH: {1:99, 2:1}, MDE: {1:60, 2:40}, MIN: {1:60, 2:40}}
{MGY: {1:85, 2:15}}
{MWH: {1:99, 2:1}}
{MGY: {1:85, 2:15}}
When I try to convert the row to a dictionary using, the following code:
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> meta = {u'CODE': u'N03', u'FABRIC': u'Jersey', u'Colour mix': u'{MGY: {1: 85, 2: 15}}', u'WEIGHT G': 165, u'Main': u'3:100', u'WEIGHT OZ': 4}
>>> colour_mix = meta['Colour mix']
>>> colour_mix
u'{MGY: {1: 85, 2: 15}}'
>>> import ast
>>> melange_items = ast.literal_eval(colour_mix)
I get the following traceback:
Traceback (most recent call last):
File "styles_fabric.py", line 194, in <module>
styleMeta = fabric_composition(style)
File "styles_fabric.py", line 185, in fabric_composition
melange_items = ast.literal_eval(dd)
File "/usr/lib/python2.7/ast.py", line 80, in literal_eval
return _convert(node_or_string)
File "/usr/lib/python2.7/ast.py", line 63, in _convert
in zip(node.keys, node.values))
File "/usr/lib/python2.7/ast.py", line 62, in <genexpr>
return dict((_convert(k), _convert(v)) for k, v
File "/usr/lib/python2.7/ast.py", line 79, in _convert
raise ValueError('malformed string')
ValueError: malformed string
what is the correct way to extract, convert the strings in dictionaries?