Python parsing: detecting duplicate keys

The yaml library in python cannot detect duplicate keys. This is a bug that was reported many years ago , and there is no fix yet.

I would like to find a worthy solution to this problem. How plausible could it be to create a regex that returns all the keys? Then it would be easy to detect this problem.

Can any regex master suggest a regex that can extract all keys to find duplicates?

Example file:

 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 
+5
source share
1 answer

The yamllint command line tool does what you want:

 sudo pip install yamllint 

In particular, it has a key-duplicates rule that detects key-duplicates and keys by rewriting each other:

 $ 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 configure.)

+5
source

Source: https://habr.com/ru/post/1235079/


All Articles