我在读一份YAML文件。如果语法错误导致异常,则将异常发送给记录器。在我的日志记录消息中,有什么方法可以识别YAML文件的哪一行包含语法错误?
try:
with open(input_path, "r") as yaml_file:
yaml_dict = yaml.load(yaml_file)
except FileNotFoundError:
logger.error("YAML file {} does not exist".format(input_path), exc_info=True)
sys.exit(1)
except:
logger.critical("Error in reading or parsing YAML file {}".format(input_path), exc_info=True)
sys.exit(1)发布于 2017-11-17 23:23:17
看看PyYAMLDocumentation,看看YAMLError()
try:
yaml.load("unbalanced blackets: ][")
except yaml.YAMLError, exc:
if hasattr(exc, 'problem_mark'):
mark = exc.problem_mark
print "Error position: (%s:%s)" % (mark.line+1, mark.column+1)
Error position: (1:22)https://stackoverflow.com/questions/47360309
复制相似问题