String manipulation

marco.minerva at gmail.com marco.minerva at gmail.com
Wed Apr 4 11:16:08 EDT 2007


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() + "|"

mwfind_re = re.compile(r"^(" + mw + ")",re.IGNORECASE|re.VERBOSE)
mwfind_subst = r"_"

for line in filein:
	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?

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




More information about the Python-list mailing list