|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: CSV to YAML Conversion in Python |
| 4 | +date: 2013-06-25 16:20:00 |
| 5 | +categories: Python |
| 6 | +description: Converting CSV into YAML is an easy task using the following Python code snippet! It helps generating proper and valid YAML-files! |
| 7 | +--- |
| 8 | + |
| 9 | +YAML Ain't Markup Language, for what reason it is often used for data-oriented, rather than document markup. With its XML-inspired structure, YAML is used widely for setting files and to store large datasets. |
| 10 | + |
| 11 | +If you have plain CSV-files but would like to work using YAML, you need something like a CVS-to-YAML converter. The following Python-Script is doing exactly this conversation and creates valid YAML: |
| 12 | + |
| 13 | +### Python-Script: Convert CSV to YAML |
| 14 | +{% highlight python %} |
| 15 | +import csv |
| 16 | +import yaml |
| 17 | + |
| 18 | +i = open('input.csv', 'r') |
| 19 | +reader = csv.DictReader(i) |
| 20 | + |
| 21 | +data = yaml.safe_dump([row for row in reader], canonical=False, |
| 22 | + encoding='utf-8', allow_unicode=True, |
| 23 | + default_flow_style=False) |
| 24 | + |
| 25 | +o = open('output.yaml', 'w') |
| 26 | +o.write(data) |
| 27 | +{% endhighlight %} |
| 28 | + |
| 29 | +In my eyes, the script is greatly self-explaining, what makes it unnecessary to bloat it with redundant comments. Nevertheless, I am going to explain some of the YAML conversion parameters. |
| 30 | + |
| 31 | +- First, the used **safe_dump** method "produces only standard YAML tags" as it is stated in the [PyYAML doc](http://pyyaml.org/wiki/PyYAMLDocumentation#DumpingYAML) and for that reason "can not represent an arbitrary Python object". |
| 32 | +- The **encoding** and **allow_uniode** parameters should be pretty straight forward. |
| 33 | +- With **canonical** enabled, the dumper explicitly defines the types of the values within the YAML. |
| 34 | +- Disabling the **default_flow_style** results in a more human-readable output. |
| 35 | + |
| 36 | +If you have any question, write a comment and I'll get back to you asap! |
| 37 | + |
| 38 | +*[JSON]: JavaScript Object Notation |
| 39 | +*[CSV]: Comma-Separated Values |
| 40 | +*[YAML]: YAML Ain't Markup Language |
| 41 | +*[XML]: Extensible Markup Language |
0 commit comments