Noé Rubinstein
Noé Rubinstein

Reputation: 910

How to add _ between group of digits in YAML using Python's PyYAML

In Python, you can make numbers more readable by adding _ between group of digits:

x = 1_234_567

However dumping with YAML will not do the same:

import yaml
yaml.dump(1_234_567) # => "1234567"

Can I make PyYAML print numbers with the digits nicely grouped?

Upvotes: 1

Views: 556

Answers (1)

Noé Rubinstein
Noé Rubinstein

Reputation: 910

You can customize the behaviour of PyYAML for any type by adding a representer:

import re
import yaml

def pretty_print_int(n):
    return re.sub(r"(?!^)(?=(?:...)+$)", "_", str(n))

def represent_int(dumper, data):
    return dumper.represent_scalar("tag:yaml.org,2002:int", pretty_print_int(data))

yaml.add_representer(int, represent_int)

The magic tag:yaml.org,2002:int value is taken from the YAML spec and known to PyYAML.

PyYAML will load the generated YAML successfully. Formatting numbers with underscores conforms to the YAML 1.1 spec but not to the YAML 1.2 spec, so some other parsers might have problems with it.

Upvotes: 2

Related Questions