在python中映射yaml文件

时间:2018-06-01 16:14:25

标签: python yaml data-science

这是一个YAML文件。它包含从股票代码到功能类别的映射列表。

以下是BANKNIFTY_O_C_0_10_W的映射:

index: [ BANKNIFTY_O_C_0_09_W: books,BANKNIFTY_O_C_0_09_W: trends,BANKNIFTY_O_C_0_09_W: trades,BANKNIFTY_O_C_0_09_W: relations,BANKNIFTY_O_P_0_09_W: books,BANKNIFTY_O_P_0_09_W: trends,BANKNIFTY_O_P_0_09_W: trades,BANKNIFTY_O_P_0_09_W: negrelations,BANKNIFTY_O_C_0_10_W: books,BANKNIFTY_O_C_0_10_W: trends,BANKNIFTY_O_C_0_10_W: trades,BANKNIFTY_O_C_0_10_W: relations,BANKNIFTY_O_C_0_10_W: options_banknifty_weekly,BANKNIFTY_O_P_0_10_W: books,BANKNIFTY_O_P_0_10_W: trends,BANKNIFTY_O_P_0_10_W: trades,BANKNIFTY_O_P_0_10_W: negrelations,BANKNIFTY_F_0: books,BANKNIFTY_F_0: trends,BANKNIFTY_F_0: trades,BANKNIFTY_F_0: relations,NIFTY_F_0: books,NIFTY_F_0: trends,NIFTY_F_0: trades,NIFTY_F_0: relations ]

我需要以下输出:

index: 
- BANKNIFTY_O_C_0_09_W: [books, trends, trades, relations]
- BANKNIFTY_O_P_0_09_W: [books, trends, trades, negrelations]
- BANKNIFTY_O_C_0_10_W: [books, trends, trades, relations, options_banknifty_weekly]
- BANKNIFTY_O_P_0_09_W: [books, trends, trades, negrelations]
- BANKNIFTY_F_0: [books, trends, trades, relations]
- NIFTY_F_0: [books, trends, trades, relations]

1 个答案:

答案 0 :(得分:0)

您的输入是单项映射,其值为单项映射列表。 您的输出是单项映射的列表。那份清单是  以相同的方式排序原始映射的键出现。这表明应使用列表或OrderedDict

收集该信息

这些映射的相应值是这些映射的键的原始值列表,也是它们出现的顺序,但至少部分重复在原始映射中,而不是在目标中。由于订单需要保留,因此无法使用set(会自动过滤双倍)。而是可以使用列表,这需要检查已经在列表中的项目。但是在下面我使用了另一个OrderedDict,被滥用为" OrderedSet"不看价值观。

假设输入位于文件input.yaml

import sys
import pathlib
from collections import OrderedDict
import ruamel.yaml

yaml_file = pathlib.Path('input.yaml')
yaml = ruamel.yaml.YAML()
yaml.default_flow_style = None 
data = yaml.load(yaml_file)
indexed = OrderedDict()
for elem in data['index']:
    for k in elem:  # just one each
        single_item_map = indexed.setdefault(k, OrderedDict())
        single_item_map[elem[k]] = None  # arbitrary value, unused
l = []
for elem in indexed:
    l.append({elem: [k for k in indexed[elem]]})
data['index'] = l
yaml.dump(data, sys.stdout)

给出:

index:
- BANKNIFTY_O_C_0_09_W: [books, trends, trades, relations]
- BANKNIFTY_O_P_0_09_W: [books, trends, trades, negrelations]
- BANKNIFTY_O_C_0_10_W: [books, trends, trades, relations, options_banknifty_weekly]
- BANKNIFTY_O_P_0_10_W: [books, trends, trades, negrelations]
- BANKNIFTY_F_0: [books, trends, trades, relations]
- NIFTY_F_0: [books, trends, trades, relations]

yaml.default_flow_style=None是必需的,因为默认情况下,实例YAML()将使用块样式,而您的输出在叶节点上具有流样式。 ruamel.yaml可以进行更精细的调整控制,方法是不使#34;正常" dicts和lists,但子类化内部用于保持往返信息的对象。在你的情况下,这不是必需的,因为你想要由.default_flow_style控制的三种模式之一(False:all-block-style,True:all-flow-style,{{1 }:block-style-with-leafs-in-flow-style)