[英]How to get required values from dict in Python
我有下一個功能:
def render(self, src, **kwargs):
dict = self._tree_read(src, self.src_tree_path)
dict包含:
[('hosts', [{'username': 'test-user', 'url': 'http://example.com', 'hostname': 'test-host'}, {'username': 'test-user2', 'url': 'http://example.com2', 'hostname': 'test-host2'}])]
然后我試圖運行另一個bash腳本並將參數從dict傳遞給它(我在dict中有2種配置,因此我需要運行2次子進程):
subprocess.call(["/bin/bash", "/opt/hosts.sh", "username", "url", "hostname"], shell=False)
但沒有成功,請幫助我。 謝謝!
您所說的dict
實際上是一個包含元組的列表,其中包含一個dict
列表。
首先, dict
是內置的,請勿覆蓋它,將其稱為其他變量。
從那里,您可以遍歷這些結果,以使用所需的值多次運行該子流程。
def render(self, src, **kwargs):
results = self._tree_read(src, self.src_tree_path)
for host in result[0][1]:
subprocess.call(["/bin/bash", "/opt/hosts.sh", host["username"], host["url"], host["hostname"]], shell=False)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.