Skip to content

Commit cad63df

Browse files
author
jbspeakr
committedJun 25, 2013
adds analytics
1 parent 7bfd3bb commit cad63df

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed
 

‎_includes/analytics.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script type="text/javascript">
2+
3+
var _gaq = _gaq || [];
4+
_gaq.push(['_setAccount', 'UA-36462674-2']);
5+
_gaq.push(['_trackPageview']);
6+
7+
(function() {
8+
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
9+
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
10+
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
11+
})();
12+
13+
</script>
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
layout: post
3+
title: CSV to JSON Conversion in Python
4+
date: 2013-06-13 17:02:16
5+
categories: Python
6+
description: How to convert a CSV-file into JSON? This Python code snippet helps you to generate a proper and valid JSON-file!
7+
---
8+
9+
When it comes to data like a list of [top level domains](https://gist.github.com/jbspeakr/4466385) (TLDs) or similar small and mostly flat datasets, I prefer to work with JSON as a fundamental data structure. The reason for that is not just its great human-readability (which is really nice to have for newcomers), but also the smooth interaction with e.g. Python and JavaScript.
10+
11+
Nevertheless it seems that the majority of open datasets is still published as classic CSV-files. This is totally fine regarding its highly compressed way of storing information, but sometimes you just want to have something much clearer.
12+
13+
For that reason, you would probably want to convert your CSV-file into a proper and valid JSON-file. The following Python-Script is doing exactly this conversation:
14+
15+
### Python-Script: Convert CSV to JSON
16+
{% highlight python %}
17+
import csv
18+
import json
19+
20+
i = open('input.csv', 'r')
21+
reader = csv.DictReader(i)
22+
data = json.dumps([row for row in reader], indent=4)
23+
24+
o = open('output.json', 'w')
25+
o.write(data)
26+
{% endhighlight %}
27+
28+
In my eyes, the script is greatly self-explaining, what makes it unnecessary to bloat it with redundant comments. If you have any question, write a comment and I'll get back to you asap!
29+
30+
*[JSON]: JavaScript Object Notation
31+
*[CSV]: Comma-Separated Values
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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

Comments
 (0)
Please sign in to comment.