#!/usr/bin/env python
# -*- Coding: utf-8 -*-

"""
" In its present form, it simply takes a comma delimited .csv file and outputs a .txt file containing valid 301 redirect statements for an .htaccess file
"
" author: Claude Müller
" wbsite: http://mediarocket.co.za
"
"""

import sys
import csv

class ThreeOhOne:
    outputDir = 'outputs/'

    def __init__(self, filename):
        self._process(sys.argv[1])

    def _process(self, filename):
        try:
            fd = open(filename, 'rt')
        except FileNotFoundError as err:
            print('Error: File not found ;/ ({0})'.format(err))

        try:
            fdOut = open(self.outputDir + filename + '.out.txt', 'w')

            reader = csv.reader(fd)
            for row in reader:
                print(row)

            fdOut.close()
        except:
            print('Error:', sys.exc_info()[0])
            raise

        fd.close()


def main():
    if len(sys.argv) < 2:
        print("usage: " + sys.argv[0] + " <the_file.csv>")
        exit(1)
    else:
        threeOhOne = ThreeOhOne(sys.argv[1])

if __name__ == "__main__":
    main()

@@ -287,16 +287,48 @@
 port csv
+%0Aimport re%0Aimport urllib.request
 %0A%0Aclass 
@@ -365,16 +365,70 @@
 utputs/'
+%0A    csvHeadings = %5B'404 URLs', 'New URL', 'Redirect'%5D
 %0A%0A    de
@@ -758,19 +758,19 @@
 out.
-txt
+csv
 ', 'w')%0A
 %0A   
@@ -769,103 +769,1459 @@
 w')%0A
-%0A            reader = csv.reader(fd)%0A            for row in reader:%0A                print(row)%0A
+            writer = csv.writer(fdOut, delimiter=',')%0A            processedRows = %5B%5D%0A%0A            reader = csv.reader(fd)%0A            for row in reader:%0A                if self.csvHeadings%5B0%5D not in row:%0A                    newRow = %5B%5D%0A                    i = 1%0A%0A                    for column in row:%0A                        if i == 2:%0A                            column = column.replace('www.', '')%0A                        elif i == 3:%0A                            # if re.search('%5ERedirect', column):%0A                            columnPieces = re.split('%5Cs%7B1%7D', column)%0A%0A                            columnPieces%5B2%5D = urllib.request.unquote(columnPieces%5B2%5D)%0A                            columnPieces%5B3%5D = urllib.request.unquote(columnPieces%5B3%5D)%0A%0A                            if ' ' in columnPieces%5B2%5D:%0A                                columnPieces%5B2%5D = '%22' + columnPieces%5B2%5D + '%22'%0A                            if ' ' in columnPieces%5B3%5D:%0A                                columnPieces%5B3%5D = '%22' + columnPieces%5B3%5D + '%22'%0A%0A                            column = ' '.join(columnPieces)%0A                        else:%0A                            column = urllib.request.unquote(column)%0A                            if ' ' in column:%0A                                column = '%22' + column + '%22'%0A%0A                        newRow.append(column)%0A                        i = i + 1%0A                    processedRows.append(newRow)%0A%0A            writer.writerows(processedRows)
 %0A   

Convert set format .csv to custom output