parser.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:ADLES 作者: GhostofGoes 项目源码 文件源码
def parse_yaml(filename):
    """
    Parses a YAML file and returns a nested dictionary containing its contents.

    :param str filename: Name of YAML file to parse
    :return: Parsed file contents
    :rtype: dict or None
    """
    try:
        # Enables use of stdin if '-' is specified
        with sys.stdin if filename == '-' else open(filename) as f:
            try:
                # Parses the YAML file into a dict
                return load(f, Loader=Loader)
            except YAMLError as exc:
                logging.critical("Could not parse YAML file %s", filename)
                if hasattr(exc, 'problem_mark'):
                    # Tell user exactly where the syntax error is
                    mark = exc.problem_mark
                    logging.error("Error position: (%s:%s)",
                                  mark.line + 1, mark.column + 1)
                else:
                    logging.error("Error: %s", exc)
                    return None
    except FileNotFoundError:
        logging.critical("Could not find YAML file for parsing: %s", filename)
        return None
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号