Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Python
String multi-replace
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Sorin Schwimmer, post: 4157767"] Hi All, I have to eliminate diacritics in a fairly large file. Inspired by [URL]http://code.activestate.com/recipes/81330/[/URL], I came up with the following code: #! /usr/bin/env python import re nodia={chr(196)+chr(130):'A', # mamaliga chr(195)+chr(130):'A', # A^ chr(195)+chr(142):'I', # I^ chr(195)+chr(150):'O', # OE chr(195)+chr(156):'U', # UE chr(195)+chr(139):'A', # AE chr(197)+chr(158):'S', chr(197)+chr(162):'T', chr(196)+chr(131):'a', # mamaliga chr(195)+chr(162):'a', # a^ chr(195)+chr(174):'i', # i^ chr(195)+chr(182):'o', # oe chr(195)+chr(188):'u', # ue chr(195)+chr(164):'a', # ae chr(197)+chr(159):'s', chr(197)+chr(163):'t' } name="R\xc3\xa2\xc5\x9fca" regex = re.compile("(%s)" % "|".join(map(re.escape, nodia.keys()))) print regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], name) But it won't work; I end up with: Traceback (most recent call last): File "multirep.py", line 25, in <module> print regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], name) File "multirep.py", line 25, in <lambda> print regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], name) TypeError: 'type' object is not subscriptable What am I doing wrong? Thanks for your advice, SxN [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
String multi-replace
Top