Good day,
tldr; Configuration read script returns single characters instead of strings.
I am writing a program that can connect to various instruments, using SCPI commands. That aside, every instrument has to be initialised differently, and new instruments should be added, therefore I made a configuration file as partly shown below;
### Digital multimeters ###
[34405A]
init : ["*RST", "*CLS", "CONF:VOLT:DC 20, MAX"]
[34401A]
init : ["*RST", "*CLS", "CONF:VOLT:DC 20, MAX"]
### Power supplies ###
[E3634A]
init : ["*RST", "*CLS", "OUTP OFF", "APPLY:P25V", "OUTP ON"]
[E3640A]
init : ["*RST", "*CLS", "OUTP OFF", "APPLY:P25V", "OUTP ON"]
[E3631A]
init : ["*RST", "*CLS", "OUTP OFF", "APPLY:P25V", "OUTP ON"]
[61602]
init : ["*RST", "*CLS", "OUTP OFF", "OUTP:PROT:CLE", "OUTP:COUP AC",
"VOLT:AC 230", "FREQ 50", "OUTP ON"]
Furthermore, I tried to read the config file;
import configparser as cp
conf = cp.ConfigParser()
print(conf.read("devices.ini"))
print(conf.sections())
conn_dev = ["34405A", "61602"]
for devices in conf.sections():
for (key, val) in conf.items(devices):
print(devices + " : " + key + " : " + val)
for commands in val:
print(commands)
What I expected is a list of commands, but it is returning single characters, and even the quotation marks. Small sample;
PPA5530 : init : ["*RST", "*CLS"]
[
"
*
R
S
T
"
,
"
*
C
L
S
"
]
How does it come that it returns single characters instead of the command as I defined it?
val
is a string. When you iterate over a string you get a single character at a time.
Why should Python automatically convert a string to whatever you want? It doesn't know what you want.
Convert to a list
using ast.literal_eval
:
>>> import ast
>>> val = '["*RST", "*CLS"]'
>>> val
'["*RST", "*CLS"]'
>>> values = ast.literal_eval(val)
>>> values
['*RST', '*CLS']
>>> for value in values:
... print(value)
*RST
*CLS