0

Lets say we have some python dictionary and we have a VBScript. We are going to call the script in python and pass the dictionary as an argument like this:

import subprocess

dict = {}
dict ["x61"] = "P11"
dict ["x62"] = "P22"
dict ["x63"] = "P33"

subprocess.call(['cscript.exe', 'H:\\public\\vbscript.vbs', dict])

In the VBScript I am trying to assign the dictionary like this:

Dim dict
dict = WScript.Arguments(0)

The (error) output that I get in the terminal is:

required: 'x61x63x62'

May be because I am looping and trying to access all items in the dict:

dict.Item(some_variable)

Shall I serialize the dict object and how can I do it?

2
  • I suspect that call's first argument can only contain strings. I suggest serializing the dictionary in a way that VB can decode later. Maybe Json? Commented Aug 21, 2014 at 13:44
  • Yeah, I think the solution will be something in that direction.. Commented Aug 21, 2014 at 13:46

1 Answer 1

3

You can't pass objects thru the command line; the arguments need to be strings. So call the .vbs with a string representation of dict and parse the parameter in the .vbs. To get you started:

.py:

import subprocess

dict = {}
dict ["x61"] = "P11"
dict ["x62"] = "P22"
dict ["x63"] = "P33"

sdict = str(dict)

subprocess.call(['cscript.exe', '../vbs/25427813.vbs', sdict])

.vbs:

Option Explicit

Function dict(s)
  WScript.Echo "***", s
  Dim tmp
  Set tmp = CreateObject("Scripting.Dictionary")
  Dim r : Set r = New RegExp
  r.Global = True
  r.Pattern = "'([^']+)': '?([^']+)'?"
  Dim m
  For Each m In r.Execute(s)
      tmp(m.SubMatches(0)) = m.SubMatches(1)
  Next
  Set dict = tmp
End Function

WScript.Echo dict(WScript.Arguments(0))("x62")

output:

python 25427813.py
*** {'x61': 'P11', 'x63': 'P33', 'x62': 'P22'}
P22

Instead of 'rolling your own' you could use an established format, e.g. JSON.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer, it really got me to the solution. Everything is working as expected now!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.