Parsing CSV Files in Python

Almost anyone doing research at some point will have to input or extract data from an Excel spreadsheet. However, it is possible to export an Excel file as a .csv file (comma separated file) and then use Python to grab all the data instantly. The Python csv module makes all this incredibly simple:

import csv
column1 = []
column2 = []
column3 = []
openfile = csv.reader(open('data.csv'))
for row in openfile:
    first, second, third = row

This script will open your .csv file, and then use the ‘.next()’ method to create an array of all the lines in your file, which you can parse through with the for loop.

Read More
Logging into PuTTY with Perl

When you use the SSH client PuTTY a lot, it can become quite tedious selecting your server and then writing in your username and password. That’s why I decided to write a short script in Perl to speed things up, and it looks like this:

use strict;
use warnings;
open my $copy, "| xclip";
print $copy Password";
close $copy;
my $external = `putty -l YourUsername -pw YourPassord`;

I’m copying my sudo password to the Linux application xclip so its ready in my clipboard, before opening PuTTY and automatically signing in. I then converted the script into an executable file using the instructions found here.

Read More
Running an R script through Python

Before I made the switch to developing on a Linux machine, I noticed that the Python module for calling R (RPy2) seemed to be having some problems on Windows. This gave me an excuse to play around with writing my own Python script to create and run an R script. As you’ll see in the code below, I’ve used the subprocess module to execute the R script I created and then pipe the results back into the terminal.

import subprocess

# rscript is the content of the R file we'll create
rscript = '''#!/usr/bin/Rscript

cat('This is a simple program') # same as c()
NumOfIterations for (i in 1:NumOfIterations) { # 1:10 is range
cat(i, 'Hello world!')
cat(' ')
r_file = open("example.r", "w")

r_file_name = "example.r"
ppath = r'C:Program FilesRR-2.15.2bini386Rscript.exe'
proc = subprocess.Popen("%s %s" % (ppath, r_file_name), stdout=subprocess.PIPE)
output =
print output
Read More