python: 与控制台程序通信
python: communication with console program
我有控制台程序 (.exe),它基本上是无限循环:
- 接受特殊格式的字符串输入(包含换行符)
- 打印特殊格式的字符串输出(包含换行符)
具体来说,控制台程序代码如下所示:
1) read integers n, m
2) n times do:
1. reak integer k
2. read k integers
3) m times do:
1. read integer k
2. read k integers
4)do whatever
5)cout n x m characters which are in ('?', '.', 'X')
6) go to (1)
这是我手工 运行 时的样子:
我想让 python 程序与之交互,但它不起作用,而且会产生非常奇怪的东西。
这是我尝试进行 1 次输入和 1 次输出的尝试。
import subprocess
process = subprocess.Popen("Program.exe", encoding='utf-8', stdin=subprocess.PIPE)
with open('test.txt', 'r') as file:
test = file.read()
test = test + '\n'
print(solver.communicate(input=test))
solver.kill()
这是结果(在开始奇怪的无限循环之前输出是正常的):
size:
20 rows. First quantity, then quantity of numbers:
20 columns. First quantity, then quantity of numbers:
.....XXXXX..........
.......XXXX.........
...XXX...XXX........
.XXXXXXX..XX........
.XXXXXXXX.XX........
XX.....XXX.XXXXX....
........XXXXXXXXXX..
....XXXXXXXXX.XXXXX.
...XXXXXXXXXXX..XXX.
..XXX...XXX.XXX..XXX
..XX...XXXXX.XXX..XX
..XX...XX..XXXXX..X.
...X..XX...XX.XXX...
......XX..XXX.XXX...
.....XXX..XX...XX...
.....XX..XX...XX....
.....XX.......X.....
....XXX.............
....XXX.............
....XXX.............
size:
20 rows. First quantity, then quantity of numbers:
20 columns. First quantity, then quantity of numbers:
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
size:
20 rows. First quantity, then quantity of numbers:
20 columns. First quantity, then quantity of numbers:
????????????????????
.
and it repeats infinitely
.
问号表示有一些输入,无论从哪里输入都需要 20 20,但我不明白它是从哪里来的。
我想要的是向控制台程序发送一个字符串并读取一个字符串的可靠方法。
我删除了控制台程序中的提示输出,它变得像不使用通信一样简单。结果如下:
import subprocess
process = subprocess.Popen("Program.exe", encoding='utf-8', stdin=subprocess.PIPE, stdout=subprocess.PIPE)
with open('test.txt', 'r') as file:
test = file.read()
test = test + '\n'
process.stdin.write(test)
process.stdin.flush()
ans = ""
for line in process.stdout:
ans = ans + process.stdout.readline()
print(ans)
solver.kill()
我有控制台程序 (.exe),它基本上是无限循环:
- 接受特殊格式的字符串输入(包含换行符)
- 打印特殊格式的字符串输出(包含换行符)
具体来说,控制台程序代码如下所示:
1) read integers n, m
2) n times do:
1. reak integer k
2. read k integers
3) m times do:
1. read integer k
2. read k integers
4)do whatever
5)cout n x m characters which are in ('?', '.', 'X')
6) go to (1)
这是我手工 运行 时的样子:
我想让 python 程序与之交互,但它不起作用,而且会产生非常奇怪的东西。
这是我尝试进行 1 次输入和 1 次输出的尝试。
import subprocess
process = subprocess.Popen("Program.exe", encoding='utf-8', stdin=subprocess.PIPE)
with open('test.txt', 'r') as file:
test = file.read()
test = test + '\n'
print(solver.communicate(input=test))
solver.kill()
这是结果(在开始奇怪的无限循环之前输出是正常的):
size:
20 rows. First quantity, then quantity of numbers:
20 columns. First quantity, then quantity of numbers:
.....XXXXX..........
.......XXXX.........
...XXX...XXX........
.XXXXXXX..XX........
.XXXXXXXX.XX........
XX.....XXX.XXXXX....
........XXXXXXXXXX..
....XXXXXXXXX.XXXXX.
...XXXXXXXXXXX..XXX.
..XXX...XXX.XXX..XXX
..XX...XXXXX.XXX..XX
..XX...XX..XXXXX..X.
...X..XX...XX.XXX...
......XX..XXX.XXX...
.....XXX..XX...XX...
.....XX..XX...XX....
.....XX.......X.....
....XXX.............
....XXX.............
....XXX.............
size:
20 rows. First quantity, then quantity of numbers:
20 columns. First quantity, then quantity of numbers:
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
????????????????????
size:
20 rows. First quantity, then quantity of numbers:
20 columns. First quantity, then quantity of numbers:
????????????????????
.
and it repeats infinitely
.
问号表示有一些输入,无论从哪里输入都需要 20 20,但我不明白它是从哪里来的。
我想要的是向控制台程序发送一个字符串并读取一个字符串的可靠方法。
我删除了控制台程序中的提示输出,它变得像不使用通信一样简单。结果如下:
import subprocess
process = subprocess.Popen("Program.exe", encoding='utf-8', stdin=subprocess.PIPE, stdout=subprocess.PIPE)
with open('test.txt', 'r') as file:
test = file.read()
test = test + '\n'
process.stdin.write(test)
process.stdin.flush()
ans = ""
for line in process.stdout:
ans = ans + process.stdout.readline()
print(ans)
solver.kill()