Simple Text Converter
July 25, 2011
This is a simple wordlist converter. I wanted to try out argparse and continue learning about python. This is the result. I really liked working with argparse and think this is a good example.
Convert wordlist text files to various formats.
optional arguments:
-h, --help show this help message and exit
-v, --verbose Increases messages being printed to stdout
-x, --xml Convert Wordlist to XML (one layer only)
-c, --csv Convert Wordlist to csv
-i INPUTFILE, --inputfile INPUTFILE
Name of file to be imported
-o OUTPUTFILE, --outputfile OUTPUTFILE
Output file name
--version show program's version number and exit
##Converter by Brad Poulton July 2011
##python v3.2
import sys,argparse
from xml.dom.minidom import parse, parseString
#Declare commandline options and setup argparse
cmdparser=argparse.ArgumentParser(description='Convert wordlist text files to various formats.', prog='Text Converter')
cmdparser.add_argument('-v','--verbose',action='store_true',dest='verbose',help='Increases messages being printed to stdout')
cmdparser.add_argument('-x','--xml',action='store_true',dest='toxml',help='Convert Wordlist to XML (one layer only)')
cmdparser.add_argument('-c','--csv',action='store_true',dest='tocsv',help='Convert Wordlist to csv')
cmdparser.add_argument('-i','--inputfile',type=argparse.FileType('r'),dest='inputfile',help='Name of file to be imported',required=True)
cmdparser.add_argument('-o','--outputfile',type=argparse.FileType('w'),dest='outputfile',help='Output file name',required=True)
cmdparser.add_argument('--version',action='version',version='%(prog)s v0.1')
#cmdparser.add_argument('-t','--inputtype',type=int,dest='inputtype',help='File type of input. (e.g. 0 - Wordlist, 1 - CSV, 2 - XML')
args = cmdparser.parse_args()
#prepare the file
def main(argv):
#parse through commandline args
args.outputfile.dest = 'test.txt'
if args.verbose:
print ('Verbose Selected')
if args.toxml:
if args.verbose:
print ('Convert to XML Selected')
converttoxml()
if args.tocsv:
if args.verbose:
print ('Convert to CSV selected')
converttocsv()
if not (args.toxml or args.tocsv):
cmdparser.error('No action requested, add -x or -c')
return 1
def converttoxml():
if args.verbose:
print ('converting to xml...')
args.outputfile.write('\n')
args.outputfile.write('\n')
for row in args.inputfile:
row=(str(row).rstrip())
args.outputfile.write(' \n')
args.outputfile.write(' %s\n' % row)
args.outputfile.write('
\n')
args.outputfile.write(' \n')
args.outputfile.close()
def converttocsv():
if args.verbose:
print ('converting to csv...')
lineoutput = ''
#wordlist to csv
for line in args.inputfile:
lineoutput+=(str(line).rstrip() + ", ")
args.outputfile.write(lineoutput)
print('Done converting ' + args.inputfile.name + ' to ' + args.outputfile.name)
if (__name__ == "__main__"):
sys.exit(main(sys.argv))
Leave a Reply