使用Ruby将嵌套的YAML解析为键值对

时间:2017-11-29 04:10:24

标签: ruby nested yaml

我希望解决使用ruby

解析这样的YAML数据
access_log:
  stored_proc: getsomething
    uses:
      usedin: some breadcrumb

我能够解析密钥对(access_log>> stored_proc>> getsomething) 但是(access_log>>使用>> usedin>>>某些面包屑)的流量问题已经使用了>>一些面包屑作为键值对。非常感谢您的帮助

1 个答案:

答案 0 :(得分:1)

您的YAML格式不正确。你不能拥有一个带有值的键和嵌套在该键下的其他东西

from collections import Counter

counter_note = Counter(note)
counter_mag = Counter(mag)

if all(counter_mag.get(word, 0) >= count for word, count in counter_note.items()):
    r = "YES, all note elements can be found in mag"
else:
    r = "NO, all note elements cannot be found in mag"

正确的结构将是

# malformed.yml
access_log:
  stored_proc: getsomething
    uses:
      usedin: some breadcrumb

# irb
YAML.load_file 'malformed.yml'
Psych::SyntaxError: (malformed.yml): mapping values are not allowed in this context at line 3 column 9
    from (irb):51