Python: Read information from .yaml












0















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



























    0















    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

























      0












      0








      0








      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 '18 at 13:29









      VroniVroni

      435




      435
























          1 Answer
          1






          active

          oldest

          votes


















          0














          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',
            autoActivateHeartbeat: false,
            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









            0














            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






























              0














              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




























                0












                0








                0







                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 '18 at 14:20









                Anthon

                31.4k1796150




                31.4k1796150










                answered Nov 21 '18 at 13:40









                VroniVroni

                435




                435
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    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

                    How to pass form data using jquery Ajax to insert data in database?

                    National Museum of Racing and Hall of Fame

                    Guess what letter conforming each word