« Days in month | Main | Setup XML Sitemap for Drupal 6 »

February 28, 2011

Execute Python script from CLI and print output

Another example Python script. The idea here is to input a CSV file and kick out a SQL statement to quickly insert the data into a database table.

Updated - see 5)

1) Create a Python Script, call it example_script.py:
- sys.argv is taken from the command line
- the Print statement will output to the terminal

#! /usr/bin/python

import csv, sys

fileName= sys.argv[1]
inFile = list(csv.reader( open(fileName, 'r') , delimiter=','))
print str(inFile)

except IOError
print 'Can\'t open file for reading.'

2) Make the file executable (taken from this forum post.)

$ sudo chmod +x example_script.py

3) Execute!
$ sudo ./example_script.py ~/Desktop/example_inputFile.csv


$ sudo python example_script.py ~/Desktop/example_inputFile.csv

4) To pass additional arguments via the command line (e.g. database name) you can add them after the execute command and alter the code a bit.

Here's a more complete example using a script I've written to read a CSV and output an SQL insert statement:

$ sudo ./example_script.py ~/Desktop/inputFile.csv [database] [table]

This script:

#! /usr/bin/python
import csv, sys

fileName= sys.argv[1]
db = sys.argv[2]
table = sys.argv[3]

#Open CSV into a list
in1 = list(csv.reader( open(fileName, 'r') , delimiter=','))

#Write INSERT, db and table, column names and begin VALUES
print "INSERT INTO {0}.{1} ".format(db, table) +"\n"
print "(" + str(in1[0]).strip('[]') + ")\n" #First row must match table fields
print "VALUES\n"

#Write the data (i.e., rows 2:End
for row in in1[1:]:
if row != in1[-1]:
print "(" + str(row).strip('[]') + "),\n"
print "(" + str(row).strip('[]') + ");\n"

except IOError:

print 'Can\'t open file for reading.'

5) To pipe the output to a text file (via the terminal) use this command:

$ sudo ./format_csv_cli_output.py ~/Desktop/inputFile.csv [database] [table] > ~/Desktop/cli_output.txt

Source: Comment by Tomosaur in this issue.

Posted by kkwaiser at February 28, 2011 09:54 AM


Login to leave a comment. Create a new account.