Seeking assistance - string processing.

Frederic Rentsch anthra.norell at vtxmail.ch
Tue Nov 14 12:26:57 EST 2006


billpaterson2006 at googlemail.com wrote:
> 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.
>
>   

Here's a suggestion:

 >>> import SE
 >>> Editor = SE.SE ('--------------------==----  img:=[[Image: 
.jpg=.jpg]] .gif=.gif]]')
 >>> Editor ('--------------------  img: .jpg .gif')    # See if it works
'------------------------  [[Image: .jpg]] .gif]]'

It works. (Add in other replacements if the need arises.)

Works linewise

 >>> for line in f:
          new_line = Editor 
(line)                                                       
          ...

Or filewise, which comes in handy in your case:

 >>> for in_filename in glob.glob (newdir+'/*.txt'):
          out_filename = in_filename.replace ('.txt','.dat')
          Editor (in_filename, out_filename)


See if that helps. Find SE here: http://cheeseshop.python.org/pypi/SE/2.3

Frederic





More information about the Python-list mailing list