【发布时间】:2020-07-26 05:23:49
【问题描述】:
我正在制作自动评分器,但我无法从标准输出中获取“test_file”的输出类型, 像我的 test_file 返回整数但标准输出总是作为字符串 附言。如果我的代码不好或者有更好的方法,请给我建议。
def check_output(test_file, result_file, in_type, out_type, input):
if check_type(in_type, input):
process1 = Popen(["python", test_file], stdin=PIPE, stdout=PIPE, stderr=PIPE)
process1.stdin.write(str(input).encode())
stdout = process1.communicate()[0]
output1 = stdout.decode()
print("Test: " + output1)
if check_type(output1, out_type):
print("YES")
def check_type(type1, type2):
return type(type1) == type(type2)
【问题讨论】:
-
打印输出没有有类型。如果您需要类型信息,则必须更改与正在评分的代码交互的方式。
-
你运行的是 python 2 还是 python 3? 3、进程的stdout是字节流。程序不能将整数写入标准输出,实际上它需要是整数的某个字节序列化版本。也许那是 8 个字节的小端。在这种情况下,
ctypes模块会有所帮助。你能告诉我们print("Test: " + output1)显示了什么吗?更好的是,不要解码标准输出并向我们展示原始字节。另外,程序写了什么,你期望得到什么? -
我希望比较来自 test_file 和 result_file 的标准输出,当两个文件的输出是字符串而不是 int 时,它可以工作。
标签: python types subprocess stdout popen