1

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?

1
  • 1
    You should use json.loads to parse something like that I think Commented Jan 19, 2015 at 15:10

3 Answers 3

4

I think you meant to wrap the key into quotes:

import ast
ast.literal_eval('{"MGY": {1: 85, 2: 15}}') # {'MGY': {1: 85, 2: 15}}

ast.literal_eval can parse string literals, but MGY itself doesn't mean anything to it.

Sign up to request clarification or add additional context in comments.

Comments

2

I think you should use JSON instead of Python represantation to keep your data as string

>>> 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}
>>> import json
>>> str = json.dumps(meta)
>>> str
'{"CODE": "N03", "FABRIC": "Jersey", "Colour mix": "{MGY: {1: 85, 2: 15}}", "WEIGHT G": 165, "Main": "3:100", "WEIGHT OZ": 4}'
>>> meta2 = json.loads(str)
>>> meta == meta2
True

JSON is designed for that purpose, so you have a better chance that authors of json package thought about all aspects of keeping data as strings. Authors of ast probably were thinking about source code.

Comments

0

Try to use Yaml for Python to deal with the un-stand json data, here is an example:

import yaml
import ast

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']
str_json_data = str(yaml.load(colour_mix))
print str_json_data
print ast.literal_eval(str_json_data)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.