#! /usr/bin/env python

import argparse, csv, time, sys

# In order to work with kive, scripts which having a inputs
# and b inputs must have a+b command line arguments, the first a
# arguments specifying paths of input files, the subsequent b
# arguments specifying the paths of where outputs are written]

scriptDescription = "Takes CSV containing (str a, int b), outputs CSV containing (str c, int d)"
parser = argparse.ArgumentParser(scriptDescription);

parser.add_argument("input_csv",help="CSV containing (str a, int b) doublets")
parser.add_argument("output_csv",help="CSV containing (int c, str d) doublets")
args = parser.parse_args()

try:
	with open(args.input_csv, "rb") as f:
		output = open(args.output_csv, "wb");

		# csv.reader() returns list inside an iterable
		# Iterables can be used in for/in blocks
		string_csv = csv.reader(f, delimiter=',');

		try:
			for i, row in enumerate(string_csv):
				time.sleep(0.5)
				if i == 0:
					# Output column names must be registered in kive
					output.write("c,d\n")
					continue
				print "Processing row {}".format(i)
				a = str(row[0])
				b = int(row[1])
				c = 2*b
				d = a
				output.write(str(c) + "," + str(d) + "\n")

		# If csv iterable method __next__() throws error, exit
		except csv.Error as e:
			print("Error at line {}: {}".format(reader.line_num, e))
			sys.exit(1)

		output.close()

	# If no errors, return with code 0 (success)
	sys.exit(0)

# Return error code 2 if file cannot be opened
except IOError as e:
	print(e)
	sys.exit(2)

@@ -36,24 +36,42 @@
 arse
-, csv, time, sys
+%0Aimport csv%0Aimport sys%0Aimport time
 %0A%0A# 
@@ -344,67 +344,37 @@
  = %22
-Takes CSV containing (str a, int b), outputs CSV containing
+Convert CSV (str a, int b) to
  (st
@@ -437,17 +437,16 @@
 ription)
-;
 %0A%0Aparser
@@ -463,32 +463,33 @@
 ent(%22input_csv%22,
+ 
 help=%22CSV contai
@@ -552,16 +552,17 @@
 ut_csv%22,
+ 
 help=%22CS
@@ -633,17 +633,20 @@
 )%0A%0Atry:%0A
-%09
+    
 with ope
@@ -675,18 +675,24 @@
 ) as f:%0A
-%09%09
+        
 output =
@@ -719,21 +719,26 @@
 v, %22wb%22)
-;
 %0A%0A
-%09%09
+        
 # csv.re
@@ -776,18 +776,24 @@
 terable%0A
-%09%09
+        
 # Iterab
@@ -825,18 +825,24 @@
  blocks%0A
-%09%09
+        
 string_c
@@ -878,21 +878,35 @@
 ',')
-;
 %0A%0A
-%09%09try:%0A%09%09%09
+        try:%0A            
 for 
@@ -942,48 +942,87 @@
 v):%0A
-%09%09%09%09time.sleep(0.5)%0A%09%09%09%09if i == 0:%0A%09%09%09%09%09
+                time.sleep(0.5)%0A                if i == 0:%0A                    
 # Ou
@@ -1066,21 +1066,36 @@
 in kive%0A
-%09%09%09%09%09
+                    
 output.w
@@ -1112,128 +1112,215 @@
 n%22)%0A
-%09%09%09%09%09continue%0A%09%09%09%09print %22Processing row %7B%7D%22.format(i)%0A%09%09%09%09a = str(row%5B0%5D)%0A%09%09%09%09b = int(row%5B1%5D)%0A%09%09%09%09c = 2*b%0A%09%09%09%09d = a%0A%09%09%09%09
+                    continue%0A                print %22Processing row %7B%7D%22.format(i)%0A                a = str(row%5B0%5D)%0A                b = int(row%5B1%5D)%0A                c = 2*b%0A                d = a%0A                
 outp
@@ -1359,18 +1359,24 @@
  %22%5Cn%22)%0A%0A
-%09%09
+        
 # If csv
@@ -1422,18 +1422,24 @@
 r, exit%0A
-%09%09
+        
 except c
@@ -1453,19 +1453,28 @@
 r as e:%0A
-%09%09%09
+            
 print(%22E
@@ -1501,22 +1501,26 @@
 .format(
-reader
+string_csv
 .line_nu
@@ -1530,11 +1530,20 @@
 e))%0A
-%09%09%09
+            
 sys.
@@ -1555,10 +1555,16 @@
 1)%0A%0A
-%09%09
+        
 outp
@@ -1575,17 +1575,20 @@
 lose()%0A%0A
-%09
+    
 # If no 
@@ -1624,17 +1624,20 @@
 uccess)%0A
-%09
+    
 sys.exit
@@ -1709,17 +1709,20 @@
 r as e:%0A
-%09
+    
 print(e)
@@ -1726,9 +1726,12 @@
 (e)%0A
-%09
+    
 sys.

Fix a bunch of PEP8 warnings in a sample script.