简体   繁体   English

Python - 使用 vbscript 获取变量

[英]Python - Acquiring variables with vbscript

I'm working on a python code that calls a VB script aiming to perform some calculations.我正在编写一个 python 代码,它调用一个旨在执行一些计算的 VB 脚本。 These calculations MUST be performed in the VB script and there is no way to migrate them to the Python one.这些计算必须在 VB 脚本中执行,并且无法将它们迁移到 Python 脚本中。

I send the variables in the VB script like this:我在 VB 脚本中发送变量,如下所示:

WScript.Echo("TEST")

And try to acquire them in the Python code as:并尝试在 Python 代码中获取它们:

p = subprocess.Popen(['cscript.exe', VB_path, nLM, nSD, nSO, nTWS, nROH, 
                      nMOH1, nMOH2, nRad1, nRad3, nLstk, nT_Shaft_EN, S_path, 
                      S_file, S_version, nkE_ref, nTC_ref, nNImax, nCost_PM, 
                      nCost_Steel, nCost_Wire, '//nologo'], stdout=subprocess.PIPE, shell=True)
out, err = p.communicate()
out

The result I get for "out" is the following:我得到的“out”结果如下:

b'Test\r\n'

My question is: how I can send several outputs with the plain variable ?我的问题是:如何使用普通变量发送多个输出? Also, as a bonus question, is there any obvious way to send an array in this kind of way from vb to the python script ?另外,作为一个额外的问题,是否有任何明显的方法可以以这种方式将数组从 vb 发送到 python 脚本?

Since text (through a stdout stream) is your method of communication, the obvious way to send multiple values (a data structure) is to serialize it in a well-known format that both communication partners can agree on.由于文本(通过 stdout 流)是您的通信方式,发送多个值(数据结构)的显而易见的方法是以通信双方都同意的众所周知的格式将其序列化。

A common way to do this would be JSON, but JSON is comparatively hard to generate with vanilla VBScript.执行此操作的常用方法是 JSON,但使用普通 VBScript 生成 JSON 相对困难。 Other obvious candidates would be CSV, or XML, or INI, but ultimately: any format you can dream up.其他明显的候选者可能是 CSV、XML 或 INI,但最终:您可以想到的任何格式。

The advantage of choosing well-known formats is that somebody else will already have written a parser for them.选择众所周知的格式的好处是其他人已经为它们编写了解析器。 Formats you come up with require that you write the parser yourself.您提出的格式要求您自己编写解析器。

Let's assume that the INI format is capable of representing your data:让我们假设 INI 格式能够表示您的数据:

[section]
key_a = value_a
key_b = value_b

This kind of output is easy to generate in VBScript, and Python has configparser to read it.这种输出在 VBScript 中很容易生成,Python 有configparser来读取它。

So we could write a couple of helpers in VBScript:所以我们可以在 VBScript 中编写几个助手:

Sub OutputSection(name)
    WScript.StdOut.WriteLine(vbNewLine & "[" & name & "]")
End Sub

Sub OutputVariable(name, value)
    Dim i
    If IsArray(value) Then
        If UBound(value) = -1 Then
            WScript.StdOut.WriteLine(name & ". = ")    ' to signify empty array
        Else
            For i = 0 To UBound(value) - 1
                OutputVariable(name & "." & i, value(i))
            Next
        End If
    Else
        WScript.StdOut.WriteLine(name & " = " & value)
    End IF
End Sub

Calling these in your script would generated neatly organized output在您的脚本中调用这些将生成整齐组织的输出

[main]
key = value

[some_array]
array.0 = value0
array.1 = value1
array.2 = value2

In Python you could add a post-processing step that turns the individual array items into lists.在 Python 中,您可以添加一个后处理步骤,将单个数组项转换为列表。

Or, you use Join() in VBScript for arrays, which would make both the Sub OutputVariable() and the parsing on the Python end simpler, but you run the risk of mangling your data if the list delimiter can occur in an array value.或者,您在 VBScript 中将Join()用于数组,这将使Sub OutputVariable()和 Python 端的解析更简单,但如果列表分隔符可能出现在数组值中,您将面临损坏数据的风险。 It's a trade-off.这是一种权衡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM