appending to beginning of line

Bengt Richter bokr at
Wed Oct 9 03:13:50 CEST 2002

On 8 Oct 2002 05:22:23 -0700, bobx at (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 "-".
--< >--------------
def prefix(line, kw):
    for badword in kw:
        where = line.find(badword)
        if where > -1 and not badword in found:
    if not found: return ['-']
    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
        kw = file(args.pop(0)).read().split()
    except Exception,e:
        print >> sys.stderr, 'Using default bad word list'
        kw = 'bad egregious'.split()
        inFile = file(args.pop(0))
        inFile = sys.stdin
    printfile(inFile, kw)

[18:03] C:\pywk\ng>python
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.
           - 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.

[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 ;-)

Bengt Richter

More information about the Python-list mailing list