[Tutor] Help with multiple substitution

vince spicer vinces1979 at gmail.com
Fri Jul 24 17:41:16 CEST 2009


not perfect or tested but

import re

chopset.reverse() #: put the longer extensions first

exts = "|".join([re.escape(x) fro x in chopset)])

for line in file:
    print exts.sub("", line)



On Fri, Jul 24, 2009 at 9:22 AM, vankayala sailakshman <
sailakshman9 at hotmail.com> wrote:

>
> Hi All,
>
> Help needed to write a python script!!!
>
> Iam a python newbie and my problem is doing multiple substitution in a text
> file using Python regular expressions. Here is my following data, I need to
> chop of all the extensions that are given in the following list and replace
> with empty string in their position. I have tried to solve for the past two
> days but couldn't succeed. Any help would be appreciated. Thanks in advance.
>
> Sai....
>
>
>  for example, for “abc22035.pr.1” i need to get “abc22035”
>
>
>  chopset = ['.p', '.pr', '.prp', '.prpp', '.ps', '.psp', '.pspp', '.s',
> '.p.1','.pr.1', '.prp.1', '.prpp.1', '.ps.1', '.psp.1','.pspp.1', '.s.1']
>
>
>  abc22035.pr
>
> abc22035.pr.1 abc21409 .ps.1
>
> abc21409.pr.1 abc21154
>
> abc21154.pr.1 abc07584_EAOEA
>
> abc07584_EAOEA.ps.1 abc20991.pr
>
> abc20991.ps. abc21156
>
> abc21156.ps.1 abc21409
>
> abc21409.ps. abc21156
>
> abc21156.pr.1 abc21408
>
> abc21408.ps.1 abc21370.psp
>
> abc21370.pr.1 abc21370
>
> abc21370.ps.1 abc22036
>
> abc22036.pr.1 abc21154
>
> abc21154.ps.1 abc22036.prpp
>
> abc22036.ps.1 abc20991
>
> abc20991.pr.1 abc21772
>
> abc21772.pr.1 abc21408
>
> abc21408.pr.1 abc07584_EAOEA
>
> abc07584_EAOEA.pr.1 abc21065
>
>
>
> ------------------------------
> Videos Get the latest video streams on movies, Try it!<http://video.msn.com/?mkt=en-in>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090724/9c21c4fd/attachment.htm>


More information about the Tutor mailing list