Python: Read information from .yaml











up vote
0
down vote

favorite












I set up a .yaml file that contains meta data for my measurement points. In this .yaml file I used nested lists and dictionaries that contain the informations, e.g.:



stations:
- XXXX:
statnr: 11111
name: NAME
name_csv: CSV
name_snowpack: NAME_SHORT
lat: 11.11111
lon: 11.22222
alt: 1111
type: TYPE
operator: OPERATOR
param:
- x1
- x2
- x3
- x4
- x5
- YYYY:
statnr: 22222
name: NAME2
name_csv: CSV2
name_snowpack: NAME_SHORT2
lat: 22.22222
lon: 22.33333
alt: 2222
type: TYPE2
operator: OPERATOR2
param:
- y1
- y2
- y3
- y4
- y5


Next I tried to read specific entries from that file.



import yaml
with open('./config/stations.yaml','r') as file:
meta = yaml.load(file)
stations = meta['stations']
print(stations[0])


This works and prints out all information about list entry 'XXXX' but if I want to only retrieve the information about the operator like I would do with a python dictionary:



print(stations[0]['operator'])


I get a: KeyError:'operator'.



So how can I address this entry or maybe entries even one level below that?
Thanks for helping!










share|improve this question


























    up vote
    0
    down vote

    favorite












    I set up a .yaml file that contains meta data for my measurement points. In this .yaml file I used nested lists and dictionaries that contain the informations, e.g.:



    stations:
    - XXXX:
    statnr: 11111
    name: NAME
    name_csv: CSV
    name_snowpack: NAME_SHORT
    lat: 11.11111
    lon: 11.22222
    alt: 1111
    type: TYPE
    operator: OPERATOR
    param:
    - x1
    - x2
    - x3
    - x4
    - x5
    - YYYY:
    statnr: 22222
    name: NAME2
    name_csv: CSV2
    name_snowpack: NAME_SHORT2
    lat: 22.22222
    lon: 22.33333
    alt: 2222
    type: TYPE2
    operator: OPERATOR2
    param:
    - y1
    - y2
    - y3
    - y4
    - y5


    Next I tried to read specific entries from that file.



    import yaml
    with open('./config/stations.yaml','r') as file:
    meta = yaml.load(file)
    stations = meta['stations']
    print(stations[0])


    This works and prints out all information about list entry 'XXXX' but if I want to only retrieve the information about the operator like I would do with a python dictionary:



    print(stations[0]['operator'])


    I get a: KeyError:'operator'.



    So how can I address this entry or maybe entries even one level below that?
    Thanks for helping!










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I set up a .yaml file that contains meta data for my measurement points. In this .yaml file I used nested lists and dictionaries that contain the informations, e.g.:



      stations:
      - XXXX:
      statnr: 11111
      name: NAME
      name_csv: CSV
      name_snowpack: NAME_SHORT
      lat: 11.11111
      lon: 11.22222
      alt: 1111
      type: TYPE
      operator: OPERATOR
      param:
      - x1
      - x2
      - x3
      - x4
      - x5
      - YYYY:
      statnr: 22222
      name: NAME2
      name_csv: CSV2
      name_snowpack: NAME_SHORT2
      lat: 22.22222
      lon: 22.33333
      alt: 2222
      type: TYPE2
      operator: OPERATOR2
      param:
      - y1
      - y2
      - y3
      - y4
      - y5


      Next I tried to read specific entries from that file.



      import yaml
      with open('./config/stations.yaml','r') as file:
      meta = yaml.load(file)
      stations = meta['stations']
      print(stations[0])


      This works and prints out all information about list entry 'XXXX' but if I want to only retrieve the information about the operator like I would do with a python dictionary:



      print(stations[0]['operator'])


      I get a: KeyError:'operator'.



      So how can I address this entry or maybe entries even one level below that?
      Thanks for helping!










      share|improve this question













      I set up a .yaml file that contains meta data for my measurement points. In this .yaml file I used nested lists and dictionaries that contain the informations, e.g.:



      stations:
      - XXXX:
      statnr: 11111
      name: NAME
      name_csv: CSV
      name_snowpack: NAME_SHORT
      lat: 11.11111
      lon: 11.22222
      alt: 1111
      type: TYPE
      operator: OPERATOR
      param:
      - x1
      - x2
      - x3
      - x4
      - x5
      - YYYY:
      statnr: 22222
      name: NAME2
      name_csv: CSV2
      name_snowpack: NAME_SHORT2
      lat: 22.22222
      lon: 22.33333
      alt: 2222
      type: TYPE2
      operator: OPERATOR2
      param:
      - y1
      - y2
      - y3
      - y4
      - y5


      Next I tried to read specific entries from that file.



      import yaml
      with open('./config/stations.yaml','r') as file:
      meta = yaml.load(file)
      stations = meta['stations']
      print(stations[0])


      This works and prints out all information about list entry 'XXXX' but if I want to only retrieve the information about the operator like I would do with a python dictionary:



      print(stations[0]['operator'])


      I get a: KeyError:'operator'.



      So how can I address this entry or maybe entries even one level below that?
      Thanks for helping!







      yaml python-3.7 pyyaml






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 at 13:29









      Vroni

      214




      214
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          Found the answer to the question myself. Obviously didn't try that thoroughly enough before..



          Instead of having a list of stations in my stations.yaml like above:



          stations: 
          - XXXX:
          statnr:1111
          ....
          - YYYY:
          statnr:2222
          ....


          I use another dictionary:



          stations: 
          XXXX:
          statnr:1111
          param:
          -x1
          -x2
          ....
          YYYY:
          statnr:2222
          param:
          -x1
          -x2
          ....


          In this way I can use:



          import yaml

          with open('./config/stations.yaml','r') as file:
          meta = yaml.load(file)
          stations = meta['stations']
          txt = stations['XXXX']['param'][0]
          print(txt)


          and get the result



          x1


          which is exactly what I was looking for.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413128%2fpython-read-information-from-yaml%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            Found the answer to the question myself. Obviously didn't try that thoroughly enough before..



            Instead of having a list of stations in my stations.yaml like above:



            stations: 
            - XXXX:
            statnr:1111
            ....
            - YYYY:
            statnr:2222
            ....


            I use another dictionary:



            stations: 
            XXXX:
            statnr:1111
            param:
            -x1
            -x2
            ....
            YYYY:
            statnr:2222
            param:
            -x1
            -x2
            ....


            In this way I can use:



            import yaml

            with open('./config/stations.yaml','r') as file:
            meta = yaml.load(file)
            stations = meta['stations']
            txt = stations['XXXX']['param'][0]
            print(txt)


            and get the result



            x1


            which is exactly what I was looking for.






            share|improve this answer



























              up vote
              0
              down vote













              Found the answer to the question myself. Obviously didn't try that thoroughly enough before..



              Instead of having a list of stations in my stations.yaml like above:



              stations: 
              - XXXX:
              statnr:1111
              ....
              - YYYY:
              statnr:2222
              ....


              I use another dictionary:



              stations: 
              XXXX:
              statnr:1111
              param:
              -x1
              -x2
              ....
              YYYY:
              statnr:2222
              param:
              -x1
              -x2
              ....


              In this way I can use:



              import yaml

              with open('./config/stations.yaml','r') as file:
              meta = yaml.load(file)
              stations = meta['stations']
              txt = stations['XXXX']['param'][0]
              print(txt)


              and get the result



              x1


              which is exactly what I was looking for.






              share|improve this answer

























                up vote
                0
                down vote










                up vote
                0
                down vote









                Found the answer to the question myself. Obviously didn't try that thoroughly enough before..



                Instead of having a list of stations in my stations.yaml like above:



                stations: 
                - XXXX:
                statnr:1111
                ....
                - YYYY:
                statnr:2222
                ....


                I use another dictionary:



                stations: 
                XXXX:
                statnr:1111
                param:
                -x1
                -x2
                ....
                YYYY:
                statnr:2222
                param:
                -x1
                -x2
                ....


                In this way I can use:



                import yaml

                with open('./config/stations.yaml','r') as file:
                meta = yaml.load(file)
                stations = meta['stations']
                txt = stations['XXXX']['param'][0]
                print(txt)


                and get the result



                x1


                which is exactly what I was looking for.






                share|improve this answer














                Found the answer to the question myself. Obviously didn't try that thoroughly enough before..



                Instead of having a list of stations in my stations.yaml like above:



                stations: 
                - XXXX:
                statnr:1111
                ....
                - YYYY:
                statnr:2222
                ....


                I use another dictionary:



                stations: 
                XXXX:
                statnr:1111
                param:
                -x1
                -x2
                ....
                YYYY:
                statnr:2222
                param:
                -x1
                -x2
                ....


                In this way I can use:



                import yaml

                with open('./config/stations.yaml','r') as file:
                meta = yaml.load(file)
                stations = meta['stations']
                txt = stations['XXXX']['param'][0]
                print(txt)


                and get the result



                x1


                which is exactly what I was looking for.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 21 at 14:20









                Anthon

                27.8k1691142




                27.8k1691142










                answered Nov 21 at 13:40









                Vroni

                214




                214






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413128%2fpython-read-information-from-yaml%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Contact image not getting when fetch all contact list from iPhone by CNContact

                    count number of partitions of a set with n elements into k subsets

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks