Skip to content

dump does not keep string type for scientific form text #886

@chalmagr

Description

@chalmagr

When processing a yaml document that has a string value that could be interpreted as a number in scientific form, the dumped value is not quoted, causing problems when composing multiple documents and rendering the outcome as such

Example:

import yaml

document = """
list:
- code: '6E01'
- code: '1234'
- code: ABCD
"""

loaded = yaml.load(document, yaml.BaseLoader)

print(loaded)

print(yaml.dump(loaded, default_flow_style=False))

print(yaml.dump(loaded, default_flow_style=True))

The above code generates the following output

{'list': [{'code': '6E01'}, {'code': '1234'}, {'code': 'ABCD'}]}

list:
- code: 6E01
- code: '1234'
- code: ABCD

{list: [{code: 6E01}, {code: '1234'}, {code: ABCD}]}

Which is incorrect, since now the first code in the list is interpreted as a number (60.0) instead of the string 6E01.

I am expecting this to be

{'list': [{'code': '6E01'}, {'code': '1234'}, {'code': 'ABCD'}]}

list:
- code: '6E01' << quoted
- code: '1234'
- code: ABCD

{list: [{code: '6E01'}, {code: '1234'}, {code: ABCD}]} << quoted '6E01'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions