non-uniform string substituion

Steve Holden steve at holdenweb.com
Wed Feb 13 20:25:23 EST 2008


Horacius ReX wrote:
> Hi,
> 
> I have a file with a lot of the following ocurrences:
> 
> denmark.handa.1-10
> denmark.handa.1-12344
> denmark.handa.1-4
> denmark.handa.1-56
> 
> ...
> 
> distributed randomly in a file. I need to convert each of this
> ocurrences to:
> 
> denmark.handa.1-10_1
> denmark.handa.1-12344_1
> denmark.handa.1-4_1
> denmark.handa.1-56_1
> 
> so basically I add "_1" at the end of each ocurrence.
> 
> I thought about using sed, but as each "root" is different I have no
> clue how to go through this.
> 
> Any suggestion ?
> 
> Thanks in advance.

First you have to tell us what characterizes the lines you want to 
process. For example, if the only requirement is that they begin with 
"denmark.handa" you could say

for line in sys.stdin:
     if line.startswith("denmark.handa"):
         line = line[:-1]+"_1\n"
     sys.stdout.write(line)

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list