Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python yaml output - float precision?

I'm using pyyaml to outputting yaml files from python dictionaries and am wondering how to set the precision or output format of the floats when they get written to files.

How do I create the yaml.dumper to handle this?

like image 601
eengineer Avatar asked Dec 19 '25 01:12

eengineer


1 Answers

According to http://pyyaml.org/browser/pyyaml/trunk/lib/yaml/representer.py pyyaml uses the canonical string representation format of repr() when dumping floats. To be more precise, the main line of code that converts floats to strings is

# If data is a finite float and not nan
value = unicode(repr(data)).lower()

Looks like you'll have to do your own string conversion of floats if you want something different. You could either convert floats to strings ahead of time in your program or write your own Representer class to initialize yaml.dumper.Dumper with.

like image 155
nwk Avatar answered Dec 20 '25 14:12

nwk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!