python – pyyaml 3.11将字典传递给迭代器?
作者:互联网
我使用以下YAML数据:
Document:
InPath: /home/me
OutPath: /home/me
XLOutFile: TestFile1.xlsx
Sheets:
- Sheet: Test123
InFile: Test123.MQSC
Server: Testsystem1
- Sheet: Test345
InFile: Test345.MQSC
Server: Testsystem2
Title:
A: "Server Name"
B: "MQ Version"
C: "Broker Version"
Fields:
A: ServerName
B: MQVersion
C: BrokerVersion
以下代码:
import yaml
class cfgReader():
def __init__(self):
self.stream = ""
self.ymldata = ""
self.ymlkey = ""
self.ymld = ""
def read(self,infilename):
self.stream = self.stream = file(infilename, 'r') #Read the yamlfile
self.ymldata = yaml.load(self.stream) #Instanciate yaml object and parse the input "stream".
def docu(self):
print self.ymldata
print self.ymldata['Sheets']
for self.ymlkey in self.ymldata['Document']: #passes String to iterator
print self.ymlkey
for sheets in self.ymldata['Sheets']: #passes Dictionary to iterator
print sheets['Sheet']
for title in self.ymldata['Title']:
print title
for fields in self.ymldata['Fields']:
print fields
打印输出是:
{'Fields': {'A': 'ServerName', 'C': 'BrokerVersion', 'B': 'MQVersion'}, 'Document': {'XLOutFile': 'TestFile1.xlsx', 'InPath': '/home/me', 'OutPath': '/home/me'}, 'Sheets': [{'Sheet': 'Test123', 'InFile': 'Test123.MQSC', 'Server': 'Testsystem1'}, {'Sheet': 'Test345', 'InFile': 'Test345.MQSC', 'Server': 'Testsystem2'}], 'Title': {'A': 'Server Name', 'C': 'Broker Version', 'B': 'MQ Version'}}
[{'Sheet': 'Test123', 'InFile': 'Test123.MQSC', 'Server': 'Testsystem1'}, {'Sheet': 'Test345', 'InFile': 'Test345.MQSC', 'Server': 'Testsystem2'}]
X
I
O
Test123
Test345
A
C
B
A
C
B
我无法找到如何控制数据传递给迭代器的方式.我想要的是将它作为字典传递,以便我可以通过键访问该值.这适用于“表格”,但我不明白为什么.文档没有清楚地描述它:http://pyyaml.org/wiki/PyYAMLDocumentation
解决方法:
在你的代码中,self.ymldata [‘Sheets’]是一个字典列表,因为你的YAML源代码:
- Sheet: Test123
InFile: Test123.MQSC
Server: Testsystem1
- Sheet: Test345
InFile: Test345.MQSC
Server: Testsystem2
是一系列映射(这是YAML文件中顶级映射的关键表的值).
其他顶级键的值都是映射(而不是映射序列),它们以Python dict的形式加载.如果你像你一样迭代一个字典,你就得到了关键值.
如果您不想迭代这些词典,那么您不应该启动for循环.您可能想要测试顶级键的值是什么,然后相应地采取行动,例如打印出从YAML文件加载的所有字典,但顶级映射除外:
import ruamel.yaml as yaml
class CfgReader():
def __init__(self):
self.stream = ""
self.ymldata = ""
self.ymlkey = ""
self.ymld = ""
def read(self, infilename):
self.stream = open(infilename, 'r') # Read the yamlfile
self.ymldata = yaml.load(self.stream) # Instanciate yaml object and parse the input "stream".
def docu(self):
for k in self.ymldata:
v = self.ymldata[k]
if isinstance(v, list):
for elem in v:
print(elem)
else:
print(v)
cfg_reader = CfgReader()
cfg_reader.read('in.yaml')
cfg_reader.docu()
打印:
{'InFile': 'Test123.MQSC', 'Sheet': 'Test123', 'Server': 'Testsystem1'}
{'InFile': 'Test345.MQSC', 'Sheet': 'Test345', 'Server': 'Testsystem2'}
{'B': 'MQVersion', 'A': 'ServerName', 'C': 'BrokerVersion'}
{'B': 'MQ Version', 'A': 'Server Name', 'C': 'Broker Version'}
{'XLOutFile': 'TestFile1.xlsx', 'InPath': '/home/me', 'OutPath': '/home/me'}
还请注意一些一般情况,你应该知道
>我使用ruamel.yaml(免责声明:我是该软件包的作者),它支持YAML 1.2(PyYAML支持2005年的1.1标准).为了您的目的,他们的行为相同.
>不要使用file()它在Python3中不可用,请使用open()
>将相同的值两次分配给同一属性是没有意义的(self.stream = self.stream = …)
>您打开的文件/流永远不会关闭,您可能需要考虑使用
with open(infilename) as self.stream:
self.ymldata = yaml.load(self.stream)
>按照惯例,类名应以大写字符开头.
标签:python,iterator,dictionary,python-2-7,pyyaml 来源: https://codeday.me/bug/20190702/1353349.html