I haven't been able to find, in the format specification mini-language documentation, a way to format numbers the way you want them.
I propose a simple hack: to replace, in the output of PyYAML, all numbers in scientific notation with their fixed-point equivalent, like this:
import re
import yaml
# only accepts Python's/PyYAML's default scientific format, on purpose:
E_REGEX = re.compile(r'(|-)([1-9])(?:.(d+))?e([+-])(d+)')
def e_to_f(match):
sf, f0, f1, se, e = match.groups()
if f1 is None: f1 = ''
f = f0 + f1
n = int(e)
if se == '-':
z = n - 1 # i.e., n - len(f0)
if z < 0:
return sf + f[:-z] + '.' + f[-z:]
else:
return sf + '0.' + '0' * z + f
else:
z = n - len(f1)
if z < 0:
return sf + f[:z] + '.' + f[z:]
else:
return sf + f + '0' * z + '.0'
e_dict = {
'example': 1.5e-15,
'another': -3.14e+16
}
e_txt = yaml.dump(e_dict, sort_keys=False)
f_txt = E_REGEX.sub(e_to_f, e_txt)
print(e_txt)
print(f_txt)
# output:
#
# example: 1.5e-15
# another: -3.14e+16
#
# example: 0.0000000000000015
# another: -31400000000000000.0
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…