在Transcrypt Python to JavaScript 编译器中,我使用以下代码通过子进程传输数据:
process = subprocess.Popen (
[node.args [1] .s],
shell = True,
stdin = subprocess.PIPE,
stdout = subprocess.PIPE
)
process.stdin.write (sourceCode.encode ('utf8'))
process.stdin.close ()
while process.returncode is None:
process.poll ()
targetCode = process.stdout.read (). decode ('utf8'). replace ('\r\n', '\n')
我的子进程是一个 Windows .bat 文件,其中包含:
python capitalize.py
capitalize.py 的内容是:
import sys
content = sys.stdin.read ()
sys.stdout.write (content.upper ())
如果变量sourceCode
最初包含:
dit
is
een
test
结果变量targetCode
将包含:
D:\activ_tosh\geatec\transcrypt\qquick\Transcrypt\transcrypt\development\experiments\pipe>python capitalize.py
D:\activ_tosh\geatec\transcrypt\qquick\Transcrypt\transcrypt\development\experiments\pipe>call D:\python36_anaconda\python.exe capitalize.py
DIT
IS
EEN
TEST
换句话说,命令的回显被附加到标准输出,正如可以预料的那样。
如果我使用 启动命令文件echo off
,则会得到回显,因此无济于事。
如何更改 subprocess.Popen 的代码(或围绕它的代码),使其targetCode
仅包含:
DIT
IS
EEN
TEST
我尝试了很多事情,包括echo off
在各个地方使用和重新分配标准输出。而且我一直在谷歌上搜索很多,但没有找到解决方案。任何人都知道如何解决这个问题?
[EDIT1] @Mahesh Karia
我试过了:
process = subprocess.Popen (
[node.args [1] .s],
shell = True,
stdout = subprocess.PIPE
)
print (111)
targetCode = process.communicate (io.StringIO (sourceCode))[0]
print (222)
这在打印 111 后挂起。
[编辑1]
我通过使用可执行文件(从 C++ 翻译)而不是 .bat 或 .py 文件作为过滤器解决了我的问题。这不会产生任何控制台回声。
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main () {
string buffer;
getline (cin, buffer, '\f');
transform (buffer.begin(), buffer.end(), buffer.begin(), ::toupper);
cout << buffer;
}
输入数据必须以终止字符结尾,在这种情况下,我使用了 '\f':
'''
<div id="StudentContainer">
{self.props.students.map((s) => (
<StudentTile student={s} key={s.searchval} />
))}
</div>
\f'''