Parsing YAML, return with line number
Here’s an improved version of puzzlet’s answer: import yaml from yaml.loader import SafeLoader class SafeLineLoader(SafeLoader): def construct_mapping(self, node, deep=False): mapping = super(SafeLineLoader, self).construct_mapping(node, deep=deep) # Add 1 so line numbering starts at 1 mapping[‘__line__’] = node.start_mark.line + 1 return mapping You can use it like this: data = yaml.load(whatever, Loader=SafeLineLoader)