从配置文件python中读取列表

时间:2017-05-03 07:31:58

标签: python list configuration visa pyvisa

美好的一天,

tldr ;配置读取脚本返回单个字符而不是字符串。

我正在编写一个可以使用SCPI命令连接到各种乐器的程序。除此之外,每个乐器都必须以不同方式初始化,并且应该添加新乐器,因此我制作了一个配置文件,部分如下所示;

### 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"]

此外,我试图阅读配置文件;

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)

我期望的是一个命令列表,但它返回的是单个字符,甚至是引号。小样本;

PPA5530 : init : ["*RST", "*CLS"]
[
"
*
R
S
T
"
,

"
*
C
L
S
"
]

如何返回单个字符而不是我定义的命令?

1 个答案:

答案 0 :(得分:0)

list是一个字符串。迭代字符串时,一次只能获得一个字符。

为什么Python会自动将字符串转换为您想要的字符串?它不知道你想要什么。

使用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

DECLARE @xml XML=
N'<Root>
  <includedFileHeader>
    <GenDatetime>2017-01-13T11:53:36</GenDatetime>
    <OFtype>PD</OFtype>
    <issuerID>ABCDE</issuerID>
    <RecordId>99</RecordId>
  </includedFileHeader>
  <includedIssuerResult>
    <issuerId>ABCDE</issuerId>
    <RecordId>33</RecordId>
    <includedPlanResult>
      <planId>2</planId>
      <insPlanId>123456789</insPlanId>
      <RecordId>22</RecordId>
      <ClassStatusType>
        <Code>A</Code>
      </ClassStatusType>
      <includedDetails>
        <DetailId>48</DetailId>
        <DetailClmId>A3456H567</DetailClmId>
        <RecordId>66</RecordId>
      </includedDetails>
      <includedDetails>
        <DetailId>74</DetailId>
        <DetailClmId>163364170257204</DetailClmId>
        <RecordId>11</RecordId>
      </includedDetails>
    </includedPlanResult>
    <includedPlanResult>
      <planId>3</planId>
      <insPlanId>343546337</insPlanId>
      <RecordId>6</RecordId>
      <ClassStatusType>
        <Code>A</Code>
      </ClassStatusType>
      <includedDetails>
        <DetailId>55</DetailId>
        <DetailClmId>A78947J780</DetailClmId>
        <RecordId>7</RecordId>
      </includedDetails>
      <includedDetails>
        <DetailId>44</DetailId>
        <DetailClmId>146545165A54</DetailClmId>
        <RecordId>8</RecordId>
      </includedDetails>
    </includedPlanResult>
  </includedIssuerResult>
</Root>';