title: Convert YAML files to CSV (Excel)
body: <p>I have two yaml files names that have similar structure but with different data. I need to parse out the ip and name of each host and put into a single csv (excel) file as three columns.</p>
  <p>sample:</p>
  <pre><code>instances:
  - host: 173.20.1.1
    timeout: 1.0
    tags:
    - ip:173.20.1.1
    - env:prod
    - type:virtual
    - name:2-base
    - hardware:server
  - host: 174.28.2.2
    timeout: 1.0
    tags:
    - ip:174.28.2.2
    - env:prod
    - type:virtual
    - name:2-game
    - hardware:server
  - host: 174.28.32.8
    timeout: 1.0
    tags:
    - ip:174.28.32.8
    - env:prod
    - type:virtual
    - name:2-play
    - hardware:server
  </code></pre>
  <p>Expected output:</p>
  <p><a href="https://i.postimg.cc/nLKrppsv/output-csv-Excel.png" rel="nofollow noreferrer">https://i.postimg.cc/nLKrppsv/output-csv-Excel.png</a></p>
  <p>I reviewed similar questions at these links but I'm stumped:</p>
  <p><a href="https://stackoverflow.com/questions/46679787/need-a-script-that-extracts-from-a-yaml-file-content-and-output-as-a-csv-file">Need a script that extracts from a yaml file content and output as a csv file</a></p>
  <p><a href="https://stackoverflow.com/questions/51880360/convert-several-yaml-files-to-csv">Convert several YAML files to CSV</a></p>
  <p>This is current code:</p>
  <pre><code>import yaml
  import csv
  import glob
  
  
  yaml_file_names = glob.glob('./*.yaml')
  
  rows_to_write = []
  
  for idx, each_yaml_file in enumerate(yaml_file_names):
      print(&quot;Processing file &quot;, idx+1, &quot;of&quot;, len(yaml_file_names), &quot;file name:&quot;, each_yaml_file)
      with open(each_yaml_file) as f:
          data = yaml.safe_load(f)
          
          for each_dict in data['instances']:
              for each_nested_dict in each_dict['host']:
                  for each_option in each_nested_dict['tags']:
                      #write to csv yaml_file_name, each_nested_dict['tags'], each_option
                      rows_to_write.append([each_yaml_file, each_nested_dict['ip'], each_option])
                      rows_to_write.append([each_yaml_file, each_nested_dict['name'], each_option])
  
  
  
  with open('output_csv_file.csv', 'w') as out:
      csv_writer = csv.writer(out, delimiter='|', quotechar=' ')
      csv_writer.writerows(rows_to_write)
      print(&quot;Output file output_csv_file.csv created&quot;)
  </code></pre>
  <p>I get this error:</p>
  <pre><code>Processing file  1 of 2 file name: .\conf.yaml
  Traceback (most recent call last):
    File &quot;C:\NGSC\yaml2csv3.py&quot;, line 21, in &lt;module&gt;
      for each_option in each_nested_dict['tags']:
  builtins.TypeError: string indices must be integers
  </code></pre>
  <p>I know there are a lot of threads on &quot;string indices must be integers&quot;, but I haven't been successful with them so far.</p>