Search code examples
pythonpyyaml

PyYAML interprets string as timestamp


It looks as though PyYAML interprets the string 10:01 as a duration in seconds:

import yaml
>>> yaml.load("time: 10:01")
{'time': 601}

The official documentation does not reflect that: PyYAML documentation

Any suggestion how to read 10:01 as a string?


Solution

  • Put it in quotes:

    >>> import yaml
    >>> yaml.load('time: "10:01"')
    {'time': '10:01'}
    

    This tells YAML that it is a literal string, and inhibits attempts to treat it as a numeric value.