Python

Posts about the Python scripting language.

Pretty Print JSON With Python

23rd December 2014

JSON is a very common data format, but reading it can be a little difficult, especially if the JSON contains very little white space. If you have Python 2.6 or above you have use the json.tool to format the JSON so that you can read it correctly. This is also a good way to validate JSON strings that you have had to hand edit before they cause errors upstream.

If you have a file called file.json, which contains a bunch of JSON output, you can use Python to format this into a readable structure in the following way.

Multi-line Comments In Python

13th April 2012

Python doesn't officially support multi-line comments, but there is a way of implementing the same functionality using an existing language construct. Single line comments in Python are written like this:

Stopping Code Execution In Python

13th April 2012

I am currently in the process of learning Python, so I thought I would start a series of mini blog posts detailing different things that I have found useful whilst learning how to use the language.

To stop code execution in Python you first need to import the sys object. After this you can then call the exit() method to stop the program running. It is the most reliable, cross-platform way of stopping code execution. Here is a simple example.