appending to beginning of line

Terry Reedy tjreedy at udel.edu
Tue Oct 8 09:21:10 EDT 2002


"Bob" <bobx at linuxmail.org> wrote in message
news:1001ff04.0210080422.1e373e13 at posting.google.com...
> # 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)   # replace with

> 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 "-".

Is this what you are looking for?

for line in inFile.xreadlines():
    ok = True
    for badword in kw:
        if line.find(badword) > -1:
            found = '%s %s\n' % (badword, line)
            print found,                # Print the result
            outFile.write(found)   # replace with
              outfile.write(found+'n') # Write the result
              ok = False
    if ok: line = '- ' + line
        print line,
        outFile.write(line)

Terry J. Reedy







More information about the Python-list mailing list