String manipulation

marco.minerva at gmail.com marco.minerva at gmail.com
Wed Apr 4 12:02:49 EDT 2007


On 4 Apr, 17:39, Alexander Schmolck <a.schmo... at gmail.com> wrote:
> All the code is untested, but should give you the idea.
>
>
>
>
>
> marco.mine... at gmail.com writes:
> > Hi all!
>
> > I have a file in which there are some expressions such as "kindest
> > regard" and "yours sincerely". I must create a phyton script that
> > checks if a text contains one or more of these expressions and, in
> > this case, replaces the spaces in the expression with the character
> > "_". For example, the text
>
> > Yours sincerely, Marco.
>
> > Must be transformated in:
>
> > Yours_sincerely, Marco.
>
> > Now I have written this code:
>
> > filemw = codecs.open(sys.argv[1], "r", "iso-8859-1").readlines()
> > filein = codecs.open(sys.argv[2], "r", "iso-8859-1").readlines()
>
> > mw = ""
> > for line in filemw:
> >    mw = mw + line.strip() + "|"
>
> One "|" too many. Generally, use join instead of many individual string +s.
>
> mwfind_re_string = "(%s)" % "|".join(line.strip() for line in filemw)
>
> > mwfind_re = re.compile(r"^(" + mw + ")",re.IGNORECASE|re.VERBOSE)
>
> mwfind_re = re.compile(mwfind_re_string),re.IGNORECASE)
>
> > mwfind_subst = r"_"
>
> > for line in filein:
>
> That doesn't work. What about "kindest\nregard"? I think you're best of
> reading the whole file in (don't forget to close the files, BTW).
>
> >    line = line.strip()
> >    if (line != ""):
> >                            line = mwfind_re.sub(mwfind_subst, line)
> >            print line
>
> > It correctly identifies the expressions, but doesn't replace the
> > character in the right way. How can I do what I want?
>
> Use the fact that you can also use a function as a substitution.
>
> print mwfind_re.sub(lambda match: match.group().replace(' ','_'),
>                     "".join(line.strip() for line in filein))
>
> 'as- Nascondi testo tra virgolette -
>
> - Mostra testo tra virgolette -

Hi Alexander!

Thank you very much, your code works perfectly!

--
Marco Minerva, marco.minerva at gmail.com
http://blogs.ugidotnet.org/marcom




More information about the Python-list mailing list