简体   繁体   English

将python字符串转换为字典

[英]converting python string to dictionary

I have an excel document with a row, something like: 我有一行的excel文档,如下所示:

{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? 提取,转换字典中的字符串的正确方法是什么?

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. ast.literal_eval 可以解析字符串文字,但是MGY本身并不意味着任何意义。

I think you should use JSON instead of Python represantation to keep your data as string 我认为您应该使用JSON而不是Python表示法来将数据保留为字符串

>>> 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. JSON是为此目的而设计的,因此您有更大的机会让json包的作者考虑将数据保留为字符串的所有方面。 Authors of ast probably were thinking about source code. ast作者可能正在考虑源代码。

Try to use Yaml for Python to deal with the un-stand json data, here is an example: 尝试使用Yaml for Python处理无法理解的json数据,这是一个示例:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM