简体   繁体   中英

Python - Acquiring variables with vbscript

I'm working on a python code that calls a VB script aiming to perform some calculations. These calculations MUST be performed in the VB script and there is no way to migrate them to the Python one.

I send the variables in the VB script like this:

WScript.Echo("TEST")

And try to acquire them in the Python code as:

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:

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 ?

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.

A common way to do this would be JSON, but JSON is comparatively hard to generate with vanilla VBScript. Other obvious candidates would be CSV, or XML, or INI, but ultimately: any format you can dream up.

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:

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

So we could write a couple of helpers in 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.

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. It's a trade-off.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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