How to round numeric output from yaml.dump, in Python?
问题 Is there a clean way to control number rounding output of yaml.dump ? For example, I have a class with different complexity variables, some of which are double precision numbers I want to be round to say 4th digit. This yaml output is for display only; it will not be loaded (i.e. yaml.load will not be used). As a naive example, consider class A below: import yaml class A: def __init__(self): self.a = 1/7 self.b = 'some text' self.c = [1/11, 1/13, 1/17, 'some more text'] def __repr__(self):