How to recursively sort YAML using CommentedMap?
问题 There's a good example of sorting at the top level, but how would you recurse the sort at all levels? This seems to work: def sort_commentedmap(self, od): res = ruamel.yaml.comments.CommentedMap() for k in sorted(od): res[k] = od[k] if not isinstance(od[k], ruamel.yaml.comments.CommentedSeq): continue for idx, i in enumerate(od[k]): if isinstance(i, str): res[k][int(idx)] = i else: res[k][int(idx)] = self.sort_commentedmap(i) return res Does this look correct? 回答1: In YAML you can have