appending to beginning of line
bokr at oz.net
Wed Oct 9 03:13:50 CEST 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 >--------------
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 for w in found]
def printfile(inFile, kw):
for line in inFile:
badones = prefix(line, kw)
print '%12s %s' % (badones, line.rstrip())
for w in badones[1:]:
print '%12s' % ('+ '+w,)
if __name__ == '__main__':
inFile = badwords = None
kw = file(args.pop(0)).read().split()
print >> sys.stderr, 'Using default bad word list'
kw = 'bad egregious'.split()
inFile = file(args.pop(0))
inFile = sys.stdin
[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.
- This is the first line.
bad This has a bad word.
- This has none.
egregious This is egregiously bad.
- This is the last line.
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 ;-)
More information about the Python-list