我目前正在研究一个课程,让我们将代码提交给自动评分器,然后返回我们的结果。它返回的格式很难在视觉上解析,所以我想编写一个可以在管道中使用的脚本,以使其更易于阅读。
这是自动评分器的输出:
Problem,Correct?,Correct Answer,Agent's Answer
"Challenge Problem B-04",0,4,-1
"Basic Problem B-12",0,1,-1
"Challenge Problem B-05",0,6,-1
"Challenge Problem B-07",0,6,-1
"Challenge Problem B-06",0,3,-1
"Basic Problem B-11",0,1,-1
"Basic Problem B-10",0,3,-1
"Challenge Problem B-03",0,3,-1
"Challenge Problem B-02",0,1,-1
"Challenge Problem B-01",0,6,-1
"Challenge Problem B-09",0,4,-1
"Challenge Problem B-08",0,4,-1
"Basic Problem B-08",0,6,-1
"Basic Problem B-09",0,5,-1
"Basic Problem B-04",0,3,-1
"Basic Problem B-05",0,4,-1
"Basic Problem B-06",0,5,-1
"Basic Problem B-07",0,6,-1
"Basic Problem B-01",0,2,-1
"Basic Problem B-02",0,5,-1
"Basic Problem B-03",0,1,-1
"Challenge Problem B-10",0,4,-1
"Challenge Problem B-11",0,5,-1
"Challenge Problem B-12",0,1,-1
{
"Basic Problems B": {
"Incorrect": "0",
"Skipped": "12",
"Correct": "0",
"Set": "Basic Problems B"
},
"Challenge Problems B": {
"Incorrect": "0",
"Skipped": "12",
"Correct": "0",
"Set": "Challenge Problems B"
}
}
它是逗号分隔值和 JSON 的混合。把这一切放在一张我可以阅读的漂亮桌子上会很好。
目前,我有类似的东西
python submit.py --provider gt --assignment error-check | column -t -s, | less -S
哪个输出:
{
"Basic Problems B": {
"Incorrect": "0",
"Skipped": "12",
"Correct": "0",
"Set": "Basic Problems B"
},
"Challenge Problems B": {
"Incorrect": "0",
"Skipped": "12",
"Correct": "0",
"Set": "Challenge Problems B"
}
}
Problem Correct? Correct Answer Agent's Answer
"Challenge Problem B-04" 0 4 -1
"Basic Problem B-12" 0 1 -1
"Challenge Problem B-05" 0 6 -1
"Challenge Problem B-07" 0 6 -1
"Challenge Problem B-06" 0 3 -1
"Basic Problem B-11" 0 1 -1
"Basic Problem B-10" 0 3 -1
"Challenge Problem B-03" 0 3 -1
"Challenge Problem B-02" 0 1 -1
"Challenge Problem B-01" 0 6 -1
"Challenge Problem B-09" 0 4 -1
"Challenge Problem B-08" 0 4 -1
"Basic Problem B-08" 0 6 -1
"Basic Problem B-09" 0 5 -1
"Basic Problem B-04" 0 3 -1
"Basic Problem B-05" 0 4 -1
"Basic Problem B-06" 0 5 -1
"Basic Problem B-07" 0 6 -1
"Basic Problem B-01" 0 2 -1
"Basic Problem B-02" 0 5 -1
"Basic Problem B-03" 0 1 -1
"Challenge Problem B-10" 0 4 -1
"Challenge Problem B-11" 0 5 -1
"Challenge Problem B-12" 0 1 -1
这让我大部分时间都在那里。现在我想知道是否有一种方法可以处理 JSON?
我不能依赖在某个行号处拆分输出,但我想我可以在输出第一次找到{
.
我想尽可能少地这样做,以便与同学分享。所以依赖越少越好。
我看过其他建议使用外部代码的 JSON 解析帖子。
理想的输出如下所示:
Problem Correct? Correct Answer Agent's Answer
"Challenge Problem B-04" 0 4 -1
"Basic Problem B-12" 0 1 -1
"Challenge Problem B-05" 0 6 -1
"Challenge Problem B-07" 0 6 -1
"Challenge Problem B-06" 0 3 -1
"Basic Problem B-11" 0 1 -1
"Basic Problem B-10" 0 3 -1
"Challenge Problem B-03" 0 3 -1
"Challenge Problem B-02" 0 1 -1
"Challenge Problem B-01" 0 6 -1
"Challenge Problem B-09" 0 4 -1
"Challenge Problem B-08" 0 4 -1
"Basic Problem B-08" 0 6 -1
"Basic Problem B-09" 0 5 -1
"Basic Problem B-04" 0 3 -1
"Basic Problem B-05" 0 4 -1
"Basic Problem B-06" 0 5 -1
"Basic Problem B-07" 0 6 -1
"Basic Problem B-01" 0 2 -1
"Basic Problem B-02" 0 5 -1
"Basic Problem B-03" 0 1 -1
"Challenge Problem B-10" 0 4 -1
"Challenge Problem B-11" 0 5 -1
"Challenge Problem B-12" 0 1 -1
Set Incorrect Skipped Correct
Basic Problems B 0 12 0
Challenge Problems B 0 12 0
将 JSON 与其他 JSON 分开非常容易。这只会给你非 JSON:
而这个,只有 JSON:
为了说明,我已将您的示例输入保存为
file
和:和
现在,您已经很好地处理了非 JSON,所以我不会改变它。理想情况下,应该使用 JSON 解析器来解析 JSON 数据,例如
jq
. 可悲的是,我没有足够的知识jq
来正确地做到这一点,所以我能想出的最好的就是这个,相当不优雅的解决方案。至少它会做你想做的事(cat file
用你的python submit.py --provider gt --assignment error-check
命令替换:将所有这些放在一个 shell 脚本中给出:
产生以下输出:
虽然感觉很老套,但我希望有人能用专用的 JSON 解析器想出一个更干净的解决方案。
Steeldriver很好地
jq
在评论中给出了一个合适的解决方案,所以如果我们把它结合起来,我们会变得更简单(也更安全):使用米勒(https://github.com/johnkerl/miller)并运行
你将会有
.