简体   繁体   中英

ruamel.yaml refuses to dump a scientific number

I have a numpy array containing numbers written in scientific notation that I want to dump using ruamel.yaml. However, I get the following error message when I try to do it:

ruamel.yaml.representer.RepresenterError: cannot represent an object: 2.702069928616081e-06

I tried to convert it into a string using astype(str) from numpy but it does not fix the problem.

ruamel.yaml has no problem loading or dumping floats in scientific notation, neither using the C library nor in roundtrip mode:

import sys
import ruamel.yaml

yaml_str = """\
- 2.702069928616081e-06
"""

for typ in ['safe', 'rt']:
   yaml = ruamel.yaml.YAML(typ=typ)
   yaml.default_flow_style = False
   data = yaml.load(yaml_str)
   print('Python:', typ, type(data[0]))
   print(data)
   print('YAML:')
   yaml.dump(data, sys.stdout)

which gives:

Python: safe <class 'float'>
[2.702069928616081e-06]
YAML:
- 2.702069928616081e-06
Python: rt <class 'ruamel.yaml.scalarfloat.ScalarFloat'>
[2.702069928616081e-06]
YAML:
- 2.702069928616081e-06

Without your actual (minimal) program to reproduce this, there is no further indication on what you might be doing wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM