appending to beginning of line

Bengt Richter bokr at oz.net
Tue Oct 8 21:13:50 EDT 2002


On 8 Oct 2002 05:22:23 -0700, bobx at linuxmail.org (Bob) wrote:

>I have this script:
>
># loop through the list and print the lines to a file 
>for line in inFile.xreadlines():
>    for badword in kw:
>        if line.find(badword) > -1:
>            found = '%s %s' % (badword, line)
>            print found            # Print the result 
>            outFile.write(found)   # Write the result
>
>This will print the badword and then the line it is on. For those
>lines that do not contain a badword I want to place a hyphen "-".
>
--< badword.py >--------------
# badword.py
def prefix(line, kw):
    found=[]
    for badword in kw:
        where = line.find(badword)
        if where > -1 and not badword in found:
            found.append((where,badword))
    if not found: return ['-']
    found.sort()
    return [w[1] for w in found]

def printfile(inFile, kw):
    for line in inFile:
        badones = prefix(line, kw)
        print '%12s %s' % (badones[0], line.rstrip())
        for w in badones[1:]:
            print '%12s' % ('+ '+w,)

if __name__ == '__main__':
    import sys
    inFile = badwords = None
    args=sys.argv[1:]
    try:
        kw = file(args.pop(0)).read().split()
    except Exception,e:
        print >> sys.stderr, 'Using default bad word list'
        kw = 'bad egregious'.split()
    try:
        inFile = file(args.pop(0))
    except:
        inFile = sys.stdin
    printfile(inFile, kw)
------------------------------

[18:03] C:\pywk\ng>python badwords.py
Using default bad word list
This is the first line.
This has a bad word.
This has none.
This is egregiously bad.
This is the last line.
^Z
           - This is the first line.
         bad This has a bad word.
           - This has none.
   egregious This is egregiously bad.
       + bad
           - This is the last line.
^Z

[18:04] C:\pywk\ng>

You could automatically walk through many files and
substitute [bleep] everywhere, backing up originals
to a backup directory.

Or if you want to hand edit, you could generate output
like a compiler error report that an editor can use to
walk you through the editing spots ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list