如何在不使用数组的情况下读取yaml dict值?

时间:2019-02-15 15:03:46

标签: python yaml

我有一个像下面的test1.yaml文件

  accounts:
  - name: account1
    dockerRegistries: 
    - set1
    - set2
    - set3
  - name: account2
    dockerRegistries: 
    - set2
    - set4
    - set5
  - name: account3
    dockerRegistries:
     - set2
     - set5
     - set4

我需要特定名称的dockerRegistries值。

阅读yaml文件后,我尝试了以下方法:

     accounts = []
     account_name = y['kubernetes']['accounts']
     for account in account_name:
        accounts.append(account['name'])
     if account['name'] == 'account1':
        print(y['kubernetes']['accounts'][0]['dockerRegistries'])     

上面的代码根据数组0,1给出值,依此类推。但是我根据if条件想要dockerRegistries的值。

如果我给

        if account['name'] == 'account1' 

我想要

        ["set2", "set4", "set5"]

我该如何实现?

1 个答案:

答案 0 :(得分:0)

您可能希望将<?xml version="1.0" encoding="utf-8"?> <FrameLayout android:id="@+id/flContainer" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ebebed" xmlns:app="http://schemas.android.com/apk/res-auto"> <ScrollView android:id="@+id/svContainer" android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true" android:fitsSystemWindows="true" tools:context=".activities.MainNavigationActivity" tools:showIn="@layout/app_bar_main_navigation"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical" android:visibility="visible"> <TextView android:id="@+id/txtNearPromotions" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="2dp" android:background="@color/colorWhite" android:text="Near by" android:textColor="@color/colorBlack" android:textSize="18sp"/> <android.support.v7.widget.RecyclerView android:id="@+id/rcvNearPromotions" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorRecyclerViewBg" android:nestedScrollingEnabled="false" android:orientation="horizontal"/> <TextView android:id="@+id/txtLatestPromotions" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="2dp" android:background="@color/colorWhite" android:text="Latest" android:textColor="@color/colorBlack" android:textSize="18sp"/> <android.support.v7.widget.RecyclerView android:id="@+id/rcvLatestPromotions" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorRecyclerViewBg" app:layout_behavior="@string/appbar_scrolling_view_behavior" android:nestedScrollingEnabled="false"/> </LinearLayout> </ScrollView> <com.dev.promotionapp.utils.VerticalTextView android:id="@+id/txtOpenDrawer" style="@style/verticalTextStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_gravity="center_vertical" android:background="@color/colorPrimaryDark" android:rotation="180" android:text=" Lahore " android:textColor="@color/colorWhite" android:textSize="18sp"/> </FrameLayout> 移到if循环的内部。像这样:

for

根据帐户的数量以及您想对它们进行的其他操作,词典可能是一个明智的解决方案:

accounts = []
account_name = y['kubernetes']['accounts']
for account in account_name:
    accounts.append(account['name'])
    if account['name'] == 'account1':
        print(account['dockerRegistries'])