[英]python script in a typescript file
how do I use my python script in a typescript file?如何在 typescript 文件中使用我的 python 脚本? Like how do I link it?
比如我如何链接它? I have to traverse a xml file which I have a python code which updates some details and wanted to use it in the typescript server file.
我必须遍历一个 xml 文件,我有一个 python 代码,它更新了一些细节并想在 typescript 服务器文件中使用它。
Like the server should update some details in a xml file.就像服务器应该更新 xml 文件中的一些细节一样。 this update functionality is implemented in python and I wanted to use it in the typescript server code.
此更新功能在 python 中实现,我想在 typescript 服务器代码中使用它。
You can run the python script in node using exec()
: https://nodejs.org/api/child_process.html#child_processexeccommand-options-callback您可以使用
exec()
在节点中运行 python 脚本: https://nodejs.org/api/child_process.html#child_processexeccommand-options-callback
A minimal example assuming you have python3
installed on the machine running your node script could be:假设您在运行节点脚本的机器上安装了
python3
的最小示例可能是:
// shell.py
print('printed in python')
// server.js
import {exec} from 'child_process'
//if you don't use module use this line instead:
// const { exec } = require('child_process')
exec('python3 shell.py', (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(stdout)
})
// using it in the shell
% node server.js
// should output this:
printed in python
This way you could traverse your XML file, change it and output it to the js/ts file, or save it as a file and just read that via your js/ts code.这样你就可以遍历你的 XML 文件,将它和 output 更改为 js/ts 文件,或者将其保存为文件并通过你的 js/ts 代码读取。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.