如何打印JSON文件的特定行号
我正在使用python
加载JSON文件并使用JSonschema
根据我准备的schema
打印错误。
我的问题是如何从循环中打印JSON文件的特定行:
errors = sorted(validator.iter_errors(jsonData[a]), key=lambda e: e.path)
for error in errors:
print(error.message, sep=", ")
我获得的输出是'lending_details'是必需的属性
,它是error.message
。
我想要打印的是:在行编号4,'Lending_details'是必需的属性
是否有一种方法来计数并显示特定行号JSON文件?
I am using python
to load a json file and using jsonschema
to print errors according to the schema
i have prepared.
My question is how do i print a specific line of a json file from a loop:
errors = sorted(validator.iter_errors(jsonData[a]), key=lambda e: e.path)
for error in errors:
print(error.message, sep=", ")
The output i get is 'lending_details' is a required property
which is the error.message
.
What i want is to print: On line number 4 ,'lending_details' is a required property
Is there a way to count and display the specific line number of a json file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常,不,因为到JSON架构评估器看到数据实例时,数据已从JSON文本解析为数据结构,并且丢失了行编号信息。
为了完成这项工作,您需要拥有一个JSON解码器,该解码器可以以JSON Schema评估器以后可以在生成错误时使用它的方式将行号与数据的每个部分相关联。例如,我可以看到一个解码器将此JSON转换为:
进入此行编号映射器:
..然后,当JSON Schema评估器正在在数据实例“/bar”上生成错误时,我们可以使用此查找表来插入。在第8行中陷入错误。
In general, no, because by the time the JSON Schema evaluator sees the data instance, the data has been parsed from JSON text into a data structure and the line number information has been lost.
To make this work, you will need to have a JSON decoder that can associate line numbers with each section of the data in a way that the JSON Schema evaluator can later make use of it when generating its errors. For example, I could see a decoder turning this JSON:
into this line number mapper:
..and then when the JSON Schema evaluator is generating an error at data instance "/bar", we can use this lookup table to insert "..at line 8" into the error.