[Tutor] string replacement

Tom Tucker tktucker at gmail.com
Mon May 21 20:43:08 CEST 2007


One way is...

CODE
############
#!/usr/bin/python

handle = file("test.txt",'a')
number = 1
for num in range(1,5):
        handle.write("TestingSome%s\n" % (number))
        handle.write("something%s-test\n" % (number))
        number += 1
handle.close()

OUTPUT
############
TestingSome1
something1-test
TestingSome2
something2-test
TestingSome3
something3-test
TestingSome4
something4-test



On 5/21/07, Andreas Kostyrka <andreas at kostyrka.org> wrote:
>
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Well, there are a number of ways to achieve this, but the classical one
> would be:
>
> """abc%(i)s
> def%(i)s
> """ % dict(i=1)
>
> Andreas
>
> Chandrashekar wrote:
> > Hi,
> >
> > I am trying to do something like this in python. Can you please help?
> >
> > handle= open('test.txt','a')
> >
> > handle.write('''
> >
> > Testsomething$i
> >
> > something$i-test
> >
> > '''
> > )
> >
> > when i write into the file, i would like to have the output like this.
> >
> > Testsomething1
> > something1-test
> > Testsomething2
> > something2-test
> > Testsomething3
> > something3-test
> > .......
> >
> > I can perform the same using a loop. But how do i append i (1,2,......n)
> > while i am writing into the file.(I mean inside the handle.write)
> >
> > Thanks,
> > Chandru
> >
> > ------------------------------------------------------------------------
> > Pinpoint customers
> > <
> http://us.rd.yahoo.com/evt=48250/*http://searchmarketing.yahoo.com/arp/sponsoredsearch_v9.php?o=US2226&cmp=Yahoo&ctv=AprNI&s=Y&s2=EM&b=50
> >who
> > are looking for what you sell.
> >
> >
> > ------------------------------------------------------------------------
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.2 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iD8DBQFGUVPtHJdudm4KnO0RAgZwAJ9XOamfWZHd1CXYGuLIlOP11p7PWwCgxPD7
> UILsdZNbknZz4zq71EuvxSs=
> =0y02
> -----END PGP SIGNATURE-----
> _______________________________________________
> 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/20070521/ce7597ea/attachment.html 


More information about the Tutor mailing list