简体   繁体   English

typescript 文件中的 python 脚本

[英]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.

相关问题 将脚本加载到TypeScript文件中? - Load Script into TypeScript File? 告诉TypeScript该文件将包含在脚本标记中? - Tell TypeScript that file will be included with script tag? 如何将 typescript 文件导入脚本标签 - How to import typescript file into script tag 在TypeScript文件中使用Google Script类型注释变量(使用clasp) - Annotate variable with Google Script type in TypeScript file (using clasp) 在 html 和 typescript 中调用脚本 - Calling a script in html and typescript 将 JS 项目转换为 Typescript 后,EJS 文件无法找到脚本文件 - EJS File having trouble finding script file after converting JS project to Typescript TypeScript的“将JavaScript输出合并到文件中”选项不会在没有///的情况下推断出正确的脚本顺序 <reference> 标签 - TypeScript's “Combine JavaScript output into file” option doesn't infer correct script order without /// <reference> tags Angular2 with Typescript,如何在延迟加载的模块中使用Plupload CDN脚本文件? - Angular2 with Typescript, how to use Plupload CDN script file in lazy-loaded module? 获取“未捕获的 ReferenceError:x 未定义”如何从另一个脚本的 typescript 捆绑 js 文件中引用数据? - Getting "Uncaught ReferenceError: x is not defined" How to reference data from a typescript bundled js file from another script? 编译Typescript以用作JSFL脚本 - Compiling Typescript to use as a JSFL script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM