Pregunta

I have a small question in passing two or more sys.argv arguments in Python. I do it using following code.

from visa import *
import sys
inst = instrument(sys.argv[1])
inst.write("*rst; status:preset; *cls")
inst.write("voltage:UNIT Vrms")
inst.write("apply:SIN %s %s" % sys.argv[2] % sys.argv[3])

But this code does not generate any answer.

But if I pass the same code with values embedded using the code

from visa import *
import sys
inst = instrument("USB0::0x164E::0x13EC::TW00008555")
inst.write("*rst; status:preset; *cls")
inst.write("voltage:UNIT Vrms")
inst.write("apply:SIN 1000, 0.5")

I get the result.

Is there something I am missing? Any help would be very useful to pass more than two sys.argv's in Python.

¿Fue útil?

Solución

Probably the string formatting is not good, you should try:

inst.write("apply:SIN %s %s" % (sys.argv[2], sys.argv[3]))

also in the second snippet there is an additional comma so maybe:

inst.write("apply:SIN %s, %s" % (sys.argv[2], sys.argv[3]))

Otros consejos

You want to pass a tuple into the string formatting:

>>> "test %s %s" % ("a", "b")
'test a b'

The way you are currently doing it doesn't work, as what you are telling it to do is do the formatting operation twice, inserting the third argument into the second one.

Do note that in new code, str.format() is the preferred method of string formatting:

>>> "test {0} {1}".format("a", "b")
'test a b'

So you would use, for example:

inst.write("apply:SIN {0}, {1}".format(sys.argv[2], sys.argv[3]))

This exists in Python 2.6 and above, and should be used there over the modulo operator.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top