如何使用PyYAML读取python元组?

时间:2016-09-18 00:32:36

标签: python yaml pyyaml

我有以下名为input.yaml的YAML文件:

cities:
  1: [0,0]
  2: [4,0]
  3: [0,4]
  4: [4,4]
  5: [2,2]
  6: [6,2]
highways:
  - [1,2]
  - [1,3]
  - [1,5]
  - [2,4]
  - [3,4]
  - [5,4]
start: 1
end: 4

我使用PyYAML加载它并按如下方式打印结果:

import yaml

f = open("input.yaml", "r")
data = yaml.load(f)
f.close()

print(data)

结果是以下数据结构:

{ 'cities': { 1: [0, 0]
            , 2: [4, 0]
            , 3: [0, 4]
            , 4: [4, 4]
            , 5: [2, 2]
            , 6: [6, 2]
            }
, 'highways': [ [1, 2]
              , [1, 3]
              , [1, 5]
              , [2, 4]
              , [3, 4]
              , [5, 4]
              ]
, 'start': 1
, 'end': 4
}

如您所见,每个城市和高速公路都表示为一个列表。但是,我希望它们被表示为元组。因此,我使用理解手动将它们转换为元组:

import yaml

f = open("input.yaml", "r")
data = yaml.load(f)
f.close()

data["cities"] = {k: tuple(v) for k, v in data["cities"].items()}
data["highways"] = [tuple(v) for v in data["highways"]]

print(data)

然而,这似乎是一个黑客。有没有办法指示PyYAML直接将它们作为元组而不是列表读取?

3 个答案:

答案 0 :(得分:8)

我不会打电话给你所做的事情。根据我的理解,您的替代方法是在YAML文件中使用特定于python的标记,以便在加载yaml文件时适当地表示它。但是,这需要你修改你的yaml文件,如果这个文件很大,可能会非常烦人而且不理想。

查看进一步说明这一点的PyYaml doc。最终,您希望在结构前面放置一个!!python/tuple,以表示您想要表示的结构。要获取您的样本数据,它希望:

YAML FILE:

cities:
  1: !!python/tuple [0,0]
  2: !!python/tuple [4,0]
  3: !!python/tuple [0,4]
  4: !!python/tuple [4,4]
  5: !!python/tuple [2,2]
  6: !!python/tuple [6,2]
highways:
  - !!python/tuple [1,2]
  - !!python/tuple [1,3]
  - !!python/tuple [1,5]
  - !!python/tuple [2,4]
  - !!python/tuple [3,4]
  - !!python/tuple [5,4]
start: 1
end: 4

示例代码:

import yaml

with open('y.yaml') as f:
    d = yaml.load(f.read())

print(d)

将输出:

{'cities': {1: (0, 0), 2: (4, 0), 3: (0, 4), 4: (4, 4), 5: (2, 2), 6: (6, 2)}, 'start': 1, 'end': 4, 'highways': [(1, 2), (1, 3), (1, 5), (2, 4), (3, 4), (5, 4)]}

答案 1 :(得分:3)

我遇到的问题与问题相同,我对这两个答案并不满意。浏览我发现的pyyaml文档时 真的有两个有趣的方法yaml.add_constructoryaml.add_implicit_resolver

隐式解析器通过将字符串与正则表达式匹配来解决必须使用!!python/tuple标记所有条目的问题。我也想使用元组语法,所以写tuple: (10,120)而不是写一个列表tuple: [10,120]然后得到 转换成元组,我个人发现非常讨厌。我也不想安装外部库。这是代码:

import yaml
import re

# this is to convert the string written as a tuple into a python tuple
def yml_tuple_constructor(loader, node): 
    # this little parse is really just for what I needed, feel free to change it!                                                                                            
    def parse_tup_el(el):                                                                                                            
        # try to convert into int or float else keep the string                                                                      
        if el.isdigit():                                                                                                             
            return int(el)                                                                                                           
        try:                                                                                                                         
            return float(el)                                                                                                         
        except ValueError:                                                                                                           
            return el                                                                                                                

    value = loader.construct_scalar(node)                                                                                            
    # remove the ( ) from the string                                                                                                 
    tup_elements = value[1:-1].split(',')                                                                                            
    # remove the last element if the tuple was written as (x,b,)                                                                     
    if tup_elements[-1] == '':                                                                                                       
        tup_elements.pop(-1)                                                                                                         
    tup = tuple(map(parse_tup_el, tup_elements))                                                                                     
    return tup                                                                                                                       

# !tuple is my own tag name, I think you could choose anything you want                                                                                                                                   
yaml.add_constructor(u'!tuple', yml_tuple_constructor)
# this is to spot the strings written as tuple in the yaml                                                                               
yaml.add_implicit_resolver(u'!tuple', re.compile(r"\(([^,\W]{,},){,}[^,\W]*\)")) 

最后通过执行:

>>> yml = yaml.load("""
   ...: cities:
   ...:   1: (0,0)
   ...:   2: (4,0)
   ...:   3: (0,4)
   ...:   4: (4,4)
   ...:   5: (2,2)
   ...:   6: (6,2)
   ...: highways:
   ...:   - (1,2)
   ...:   - (1,3)
   ...:   - (1,5)
   ...:   - (2,4)
   ...:   - (3,4)
   ...:   - (5,4)
   ...: start: 1
   ...: end: 4""")
>>>  yml['cities']
{1: (0, 0), 2: (4, 0), 3: (0, 4), 4: (4, 4), 5: (2, 2), 6: (6, 2)}
>>> yml['highways']
[(1, 2), (1, 3), (1, 5), (2, 4), (3, 4), (5, 4)]

save_loadload相比可能存在潜在的缺点,我没有测试过。{/ p>

答案 2 :(得分:2)

根据您的“hack”来自YAML输入的位置是一个很好的解决方案,特别是如果您使用yaml.safe_load()而不是不安全的yaml.load()。如果只有YAML文件中的“叶子”序列需要是元组,则可以执行¹:

import pprint
import ruamel.yaml
from ruamel.yaml.constructor import SafeConstructor


def construct_yaml_tuple(self, node):
    seq = self.construct_sequence(node)
    # only make "leaf sequences" into tuples, you can add dict 
    # and other types as necessary
    if seq and isinstance(seq[0], (list, tuple)):
        return seq
    return tuple(seq)

SafeConstructor.add_constructor(
    u'tag:yaml.org,2002:seq',
    construct_yaml_tuple)

with open('input.yaml') as fp:
    data = ruamel.yaml.safe_load(fp)
pprint.pprint(data, width=24)

打印:

{'cities': {1: (0, 0),
            2: (4, 0),
            3: (0, 4),
            4: (4, 4),
            5: (2, 2),
            6: (6, 2)},
 'end': 4,
 'highways': [(1, 2),
              (1, 3),
              (1, 5),
              (2, 4),
              (3, 4),
              (5, 4)],
 'start': 1}

如果您需要再次处理序列需要为“普通”列表的更多资料,请使用:

SafeConstructor.add_constructor(
    u'tag:yaml.org,2002:seq',
    SafeConstructor.construct_yaml_seq)

¹这是使用ruamel.yaml YAML 1.2解析器完成的,我是作者。如果您只需要支持YAML 1.1和/或由于某种原因无法升级,您应该能够使用旧的PyYAML