View Single Post
  #3 (permalink)  
Old 02-14-2008, 02:37 AM
7stud
Guest
 
Posts: n/a
Default Re: non-uniform string substituion

On Feb 13, 4:03*pm, Horacius ReX <horacius....@gmail.com> 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
>


Is each one of those a line in the file? Or are those words
distributed throughout the file? If each one is a line, try this:

import os

input = open('data.txt')
output = open('temp.txt', 'w')

for line in input:
if line.startswith('denmark.handa.'):
output.write('%s_1\n' % line.strip())
else:
output.write(line)

os.remove('data.txt')
os.rename('temp.txt', 'data.txt')
Reply With Quote