It wasn't me. You can't prove anything.


2010-06-29

Convert text files to CSV

#!/bin/env python
#############################################################################
# Parameters:     None
# Note:           2010-06-28
# This script helped with the software key (Microsoft) audit. It converts
# the multiple text files from "keyfinder.exe" to one cvs format file that
# is imported in to a spreadsheet and edited manually for clarity. I still
# had to hand remove a couple lines after running this script. This script
# was aimed at 98% success.
#############################################################################

import os
import sys
import glob

fileExt="*.txt"
fileList=""
outputName="output.csv"
fileList=glob.glob(str(fileExt))
if (len(fileList) == 0):
    print("Error - Could not obtain "+str(fileExt)+" files.")
    sys.exit(0)
if (os.path.exists(str(outputName))):
    os.remove(str(outputName))
outputPointer=open(str(outputName),"w")
for fileName in fileList:
    computerName=fileName.rstrip(str(fileExt))
    filePointer=open(str(fileName),"r")
    fileContents=filePointer.readlines()
    filePointer.close()
    outputString=""
    for contentLine in fileContents:
        contentLine=contentLine.strip("\n")
        if (str(contentLine) == "Professional"):
            outputString+=" Professional"
            continue
        if (str(contentLine) == "Home Edition"):
            outputString+=" Home Edition"
            continue
        if (len(str(contentLine)) == 0):
            outputString+=("\n"+str(computerName))
        elif (str(contentLine).find("Magical") == -1 and str(contentLine).find("magical") == -1):
            outputString+=","+str(contentLine)
    outputString=outputString.rstrip("\n"+str(computerName))
    outputString=outputString.strip("\n\n")
    outputPointer.write(str(outputString)+"\n")
outputPointer.close()

I know, I know. It is a lame script. It works for the most part. It converts a bunch of multiple line text files in to a single CVS (comma delimited) file for import in to a spreadsheet.

No comments: