Seeking assistance - string processing.

billpaterson2006 at googlemail.com billpaterson2006 at googlemail.com
Tue Nov 14 05:32:06 EST 2006


I've been working on some code to search for specific textstrings and
act upon them insome way. I've got the conversion sorted however there
is 1 problem remaining.

I am trying to work out how to make it find a string like this "==="
and when it has found it, I want it to add "===" to the end of the
line.

For example.

The text file contains this:

===Heading

and I am trying to make it be processed and outputted as a .dat file
with the contents

===Heading===

Here's the code I have got so far.

import string
import glob
import os

mydir = os.getcwd()
newdir = mydir#+"\\Test\\";

for filename in glob.glob1(newdir,"*.txt"):
    #print "This is an input file: " + filename
    fileloc = newdir+"\\"+filename
    #print fileloc

    outputname = filename
    outputfile = string.replace(outputname,'.txt','.dat')
    #print filename
    #print a

    print "This is an input file: " + filename + ".  Output file:
"+outputfile

    #temp = newdir + "\\" + outputfile
    #print temp


    fpi = open(fileloc);
    fpo = open(outputfile,"w+");

    output_lines = []
    lines = fpi.readlines()

    for line in lines:
        if line.rfind("--------------------") is not -1:
            new = line.replace("--------------------","----")
        elif line.rfind("img:") is not -1:
            new = line.replace("img:","[[Image:")
        elif line.rfind(".jpg") is not -1:
            new = line.replace(".jpg",".jpg]]")
        elif line.rfind(".gif") is not -1:
            new = line.replace(".gif",".gif]]")
        else:
            output_lines.append(line);
            continue
        output_lines.append(new);

for line in output_lines:
    fpo.write(line)

fpi.close()
fpo.flush()
fpo.close()


I hope this gets formatted correctly :-p

Cheers, hope you can help.




More information about the Python-list mailing list