On Fri, Mar 25, 2011 at 5:45 PM, Vishal <[email protected]> wrote: > On Fri, Mar 25, 2011 at 3:31 PM, Senthil Kumaran <[email protected]>wrote: > >> On Fri, Mar 25, 2011 at 03:22:02PM +0530, Vishal wrote: >> >> > I have a string that can be many megabytes worth memory size, and i >> > want to >> >> If you have got that string from a file like object, instead of >> loading the entire string in memory you could iterate through the file >> object to load one line at time. This will help provided the string is >> has line-separators. >> >> Otherwise you could store in a mock StringIO file like object and >> iterate, if the string is heavy to handle in memory. >> >> > >> > Now I would like to chain these replacements into something like this: >> > >> > >>> s1 = s.replacements((' a', 'S'), (' \t', '\n'), ('C#', '\n'))) >> > >> > Is there a known way of doing this currently in Python, apart from >> producing >> >> Here is one recipe to do multiple string replacement in python in a >> single shot. >> >> >> http://uthcode.sarovar.org/pycon2010/strings.html#making-multiple-replacements-in-a-string-in-a-single-pass >> >> -- >> Senthil >> _______________________________________________ >> BangPypers mailing list >> [email protected] >> http://mail.python.org/mailman/listinfo/bangpypers >> > > Thanks for his one. This is what I was looking for. > > -- > Thanks and best regards, > Vishal Sapre >
Hi, Just realized after using the above solution : http://uthcode.sarovar.org/pycon2010/strings.html#making-multiple-replacements-in-a-string-in-a-single-pass def multiple_replace(text, adict): rx = re.compile('|'.join(map(re.escape, adict))) def one_xlat(match): return adict[match.group(0)] return rx.sub(one_xlat, text) that *this one is not* what I was looking for. The use of '|' character in the regular expression, makes sure that the sequence of replacements is not preserved, which is exactly what I want to avoid. Is there another way to making (compiling) the regex such that match happens as per a given sequence, and not as per occurrence of pattern in the given string. -- Thanks and best regards, Vishal Sapre _______________________________________________ BangPypers mailing list [email protected] http://mail.python.org/mailman/listinfo/bangpypers
