replace the last line using re.compile()

I’d like to implement a way of replacing the last characters of these street suffixes such as:

LINCOLN ST
LINCOLN STREET
CHESTNUT ST

Results needed:

LINCOLN
LINCOLN
CHESTNUT

However my results are:

LINCOLN
LINCOLN
CHENUT

I’ve been researching using wildcards and
`lines = file.readlines()

lines = lines[:-1]`

Please! I need help

“”" http://emilics.com/blog/article/multi_replace.html
“”"
import re
target = ‘’’

LINCOLN ST
LINCOLN STREET
CHESTNUT ST
‘’’
rdict = {
‘ST’: ‘’,
‘STREET’: ‘’,
}
robj = re.compile(’|’.join(rdict.keys()))
result = robj.sub(lambda m: rdict[m.group(0)], target)
print result

As this isn't a wxPython-specific issue, you may have more luck posting
your question on StackOverflow or similar if you haven't already.

···

--
James Scholes
http://twitter.com/JamesScholes

George McCown wrote:

I'd like to implement a way of replacing the last characters of these
street suffixes such as:

LINCOLN ST
LINCOLN STREET
CHESTNUT ST

Results needed:

LINCOLN
CHESTNUT

However my results are:

LINCOLN
CHENUT

I've been researching using wildcards and
>lines =file.readlines()
lines =lines[:-1]|

Please! I need help

""" http://emilics.com/blog/article/multi_replace.html
"""
import re
target = '''

LINCOLN ST
LINCOLN STREET
CHESTNUT ST
'''
rdict = {
'ST': '',
'STREET': '',
}
robj = re.compile('|'.join(rdict.keys()))
result = robj.sub(lambda m: rdict[m.group(0)], target)
print result

Your regex can specify that only values at the end of lines match by using '$'.

···

--
Robin Dunn
Software Craftsman