Numbers in scientific notation without dot are parsed as string
See original GitHub issueHi,
it appears that PyYAML requires a dot in the mantissa as well as a sign in the exponent in order to parse numbers in scientific notation as numbers and otherwise parses them as string:
>>> import yaml
>>> yaml.safe_load('1e+10')
'1e+10'
>>> yaml.safe_load('1.e+10')
10000000000.0
>>> yaml.safe_load('1.0e10')
'1.0e10'
According to YAML 1.2 spec the dot is optional as in JSON:
Either
0
,.inf
,-.inf
,.nan
, or scientific notation matching the regular expression-? [1-9] ( \. [0-9]* [1-9] )? ( e [-+] [1-9] [0-9]* )?
.
Furthermore, JSON makes even the sign in the exponent optional: https://www.json.org/number.gif
Since YAML 1.2 aspires to be a superset of JSON, I believe it should make the sign optional as well.
Others than ran into similar problems:
https://stackoverflow.com/questions/30458977/yaml-loads-5e-6-as-string-and-not-a-number
Best, Thomas
Issue Analytics
- State:
- Created 5 years ago
- Reactions:29
- Comments:12 (4 by maintainers)
Top Results From Across the Web
YAML loads 5e-6 as string and not a number - Stack Overflow
The problem lies in the fact that the YAML Resolver is set up to match floats as follows: Resolver.add_implicit_resolver( u'tag:yaml.org,2002:float', ...
Read more >Parsing from scientific notation - MSDN - Microsoft
Im trying to read in values from a file containing numbers that are a mix of "normal" doubles (i.e. 3.23124) and numbers written...
Read more >SQL Format Models - Snowflake Documentation
Parsing strings containing numbers is affected by both the FX and FM modifiers: In lax mode: Digit group separators are optional (i.e. numbers...
Read more >Floating point numbers - Manual - PHP
Converting to float ¶. From strings ¶. If the string is numeric or leading numeric then it will resolve to the corresponding float...
Read more >DecimalFormat (Java Platform SE 7 ) - Oracle Help Center
DecimalFormat can be instructed to format and parse scientific notation only ... Formats a number and appends the resulting text to the given...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Issue is still present, I use the fix found on Stackoverflow, manually adding the new implicit resolver.
For anyone running into similar issues, the derived package ruamel.yaml resolves issues like the one stated above.