Categories
Uncategorized

Substituting words with lists and dictionaries – Python

So we have the following string Except that it does not paint a good picture of Brighton, and The Man doesn’t like it. There are two things that we can do. We can substitute all the negative words with the same item using a list, or we can swap the negative words with nicer ones […]

Categories
Uncategorized

Sorting Lists for Lorenz Curve – Python

EDIT: This does not really work. Needs lots and lots of fixes. Use R instead. This function will take a list of 100+ numbers, divide them into percentiles, and return a list that can be used to plot a Lorenz Curve. The Function Running the Function First, we make a list full of integers. This […]

Categories
Uncategorized

Dot Graph for Directory – Python

How to create a dot graph for a directory. The Problem I wrote the dir2dot( ) python function in my previous post…but it has one massive mistake. If we have items in different folders that have the same name, the function gets all confused and freaks out. For example, if we have the following directory: […]

Categories
Uncategorized

Formatting Dates – Python

We have a date like this 3rd April 2013 and we want to change it to this 2013-04-03 We can change the date format using the datetime module. In this module we can use strptime( ) to tell Python that the string is a date, and then strftime( ) to change the date to the […]

Categories
Uncategorized

Renaming Files in Directory – Python

This script will look for all the files in a directory that end with the extension .txt and change them all to .html using the rename function in the os module. The script was written for a directory with the following structure Also, I am sure there is a more eficient way of getting the […]

Categories
Uncategorized

Get Links From All Files – Python

I have a bunch of files containing wikipedia urls in different folders, and I want to walk through them and copy these into one single file per folder. This is the script. I don’t think it’s that great, but it does work. Then, to run it, all you need to do is this

Categories
Uncategorized

Coding and Decoding urls – Python

Url addresses can’t have special characters, so some of these are encoded. An example would be a url that contains spaces, such as Which then gets encoded to More on url encoding here: http://www.w3schools.com/tags/ref_urlencode.asp What this means is that if we want to have a list of addresses which have url, you may end up […]

Categories
Uncategorized

Random d3js Network Graph – Python & d3js

I was looking for something similar to igraph’s erdos.renyi.game(n, p.or.m) that would allow me to create a random network graph and export it as a json file with the following format. Unfortunately I could’t fing anything, so I wrote the following python script that does just that

Categories
Uncategorized

Reading Files – Python

Just a quickie on files and how to read them. To read a file you can use with open(), adding then the path to the file and giving it a name. Then you can read the file using either read() or readlines(). Read() Using read() will mean that the entire file will be read and […]

Categories
Uncategorized

MySQL query in Python – Python

Just for my own reference. Install the mysql Module Connect and make query Very, very, very, very basic. More info: MySQL Python tutorial