在Python中解析Yaml:检测重复的密钥
The yaml
library in python is not able to detect duplicated keys. This is a bug that has been reported years ago and there is not a fix yet.
python中的yaml库无法检测重复的密钥。这是几年前报告的一个错误,还没有修复。
I would like to find a decent workaround to this problem. How plausible could be to create a regex
that returns all the keys ? Then it would be quite easy to detect this problem.
我想为这个问题找到一个合适的解决方法。创建一个返回所有键的正则表达式有多合理?然后很容易发现这个问题。
Could any regex master suggest a regex that is able to extract all the keys to find duplicates ?
任何正则表达式大师都可以建议一个能够提取所有密钥以查找重复项的正则表达式吗?
File example:
mykey1:
subkey1: value1
subkey2: value2
subkey3:
- value 3.1
- value 3.2
mykey2:
subkey1: this is not duplicated
subkey5: value5
subkey5: duplicated!
subkey6:
subkey6.1: value6.1
subkey6.2: valye6.2
1 个解决方案
#1
5
The yamllint command-line tool does what you want:
yamllint命令行工具可以满足您的需求:
sudo pip install yamllint
Specifically, it has a rule key-duplicates
that detects repetitions and keys over-writing one another:
具体来说,它有一个规则密钥重复项,可以检测重复和密钥互相覆盖:
$ yamllint test.yaml
test.yaml
1:1 warning missing document start "---" (document-start)
10:5 error duplication of key "subkey5" in mapping (key-duplicates)
(It has many other rules that you can enable/disable or tweak.)
(它还有许多其他规则可以启用/禁用或调整。)
更多相关文章
- ssh 免密码登录 与 密钥公钥原理讲解
- SQLSERVER的中文排序规则
- mysql自定义排序规则函数——field()
- java 对称加密——密钥与加密后的数据存盘方式
- JavaScript:undefined!=false之解 及==比较的规则
- Java中的命名规则
- yahoo前端优化34条规则——JavaScript和CSS篇