# Extract data from yml to csv

**URL:** <https://discuss.python.org/t/extract-data-from-yml-to-csv/20711>\
**Category:** Python Help\
**Created:** [November 3, 2022, 4:30pm UTC](https://discuss.python.org/t/extract-data-from-yml-to-csv/20711 "2022-11-03T16:30:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vendant2006](https://avatars.discourse-cdn.com/v4/letter/v/6f9a4e/32.png) [@vendant2006](https://discuss.python.org/u/vendant2006)\
**Post date:** [November 3, 2022, 4:31pm UTC](https://discuss.python.org/t/extract-data-from-yml-to-csv/20711/1 "2022-11-03T16:31:00Z")

</div>

Hi All, I am trying to extract the details from a yaml file and put into a csv file format as below:

> Entity Name, Attribute Name  
> “Order”,“Customer Id”  
> “Order”,“Order Date”  
> “Order Items”,“Order Id”  
> “Order Items”,“Product Id”  
> “Order Items”,“Order Item Id”  
> “Order Items”,“Unit Price”  
> “Order Items”,“Unit Quantity”

Here is the sample input yml file.

> swagger: “1.0”  
> info:  
> title: Order Details  
> version: 0.0.1  
> paths:  
> /structure:  
> post:  
> description: Generates JSON structure based on this model  
> consumes:  
> - application/json  
> produces:  
> - application/json  
> parameters:  
> - in: body  
> name: payload  
> description: generate structure  
> schema:  
> $ref: ‘#/definitions/payload’  
> responses:  
> 200:  
> description: “Okay”  
> definitions:  
> payload:  
> type: object  
> properties:  
> order:  
> type: array  
> items:  
> $ref: ‘#/definitions/modelDefinition’  
> modelDefinition:  
> type: object  
> properties:  
> order\_items:  
> $ref: ‘#/definitions/order\_items’  
> customer id:  
> type: string  
> description: “Unique Identfier of Customer”  
> order date:  
> type: string  
> description: “Date Ordered”
> 
> ### References start below
> 
> order\_items:  
> type: object  
> description: “”  
> properties:  
> order id:  
> type: string  
> description: “Unique Order ID”  
> order item id:  
> type: integer  
> description: “Unique Order Item”  
> product id:  
> type: integer  
> description: “Unique Product ID”  
> unit price:  
> type: string  
> description: “Price”  
> unit quantity:  
> type: string  
> description: “Quantity”  
> I created a python object using below code:

# Imports the YAML module for use in our script

import yaml  
#Opens the file ex1.yaml, and loads the contents in the variable ‘result’  
with open(‘test.yaml’) as f:  
data = yaml.safe\_load(f)  
print(data)

But I am not been able to get the required output looping through the nested dictionary created.  
I also tried to extract the keys by iterating through data.items() but it is giving me only few keys.

Is there a way to get the desired result ? Thanks for your help on this.

---

<div class="post-metadata">

**Author:** ![abdnafees](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/abdnafees/32/9551_2.png) [@abdnafees](https://discuss.python.org/u/abdnafees)\
**Post date:** [November 3, 2022, 6:40pm UTC](https://discuss.python.org/t/extract-data-from-yml-to-csv/20711/2 "2022-11-03T18:40:26Z")

</div>

Hey,

This might be helpful.

> **[Python YAML: How to Load, Read, and Write YAML](https://python.land/data-processing/python-yaml)**
>
> YAML is easy to write for humans, and read for computers. Learn how to open, parse, and read YAML with Python. With lots of example code!

---

<div class="post-metadata">

**Author:** ![vendant2006](https://avatars.discourse-cdn.com/v4/letter/v/6f9a4e/32.png) [@vendant2006](https://discuss.python.org/u/vendant2006)\
**Post date:** [November 4, 2022, 5:37am UTC](https://discuss.python.org/t/extract-data-from-yml-to-csv/20711/3 "2022-11-04T05:37:01Z")

</div>

Thanks for the link. I followed this link but did not help to solve my problem

---

<div class="post-metadata">

**Author:** ![vovavili](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vovavili/32/38249_2.png) [@vovavili](https://discuss.python.org/u/vovavili)\
**Post date:** [November 4, 2022, 6:28am UTC](https://discuss.python.org/t/extract-data-from-yml-to-csv/20711/4 "2022-11-04T06:28:56Z")

</div>

Use `yaml.safe_load` through a context manager to convert a YAML file to JSON-style dictionary, convert in into a dataframe using [`pandas.json_normalize`](https://pandas.pydata.org/docs/reference/api/pandas.json_normalize.html), narrow it down to columns you like using [`df.loc`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.loc.html) and export it to CSV using [`df.to_csv`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_csv.html).
