Thank you very much..:) On Tue, Jun 2, 2009 at 1:22 PM, W W <sri...@gmail.com> wrote:
> On Tue, Jun 2, 2009 at 12:42 PM, jyotsna guleria < > jyotsna.gule...@gmail.com> wrote: > >> >> Hello every one, >> >> I am trying to parse a file: >> >> I want to convert all the spaces in between the characters to single tab. >> >> e.g: my file has contents like: >> >> 1G5 79011 1 0 2 0 0 0 >> 0 0 0 0 >> 5Ht-2 60459 1 1 0 0 0 0 >> 0 0 0 0 >> >> >> I want them to be separated by a single tab not with spaces.. >> >> It should look like: >> >> 1G5 79011 1 0 2 0 0 0 0 0 0 0 >> 5Ht-2 60459 1 1 0 0 0 0 0 0 0 0 >> >> each separated by Tab... >> >> It is a text file containing such a data .. >> > > Easiest way I know of goes something like this: > > for line in myfile: > newline = '\t'.join(line.split()) > > Consider: > > In [16]: x = 'the quick brown fox ate some spam and eggs' > > In [17]: x.split() > Out[17]: ['the', 'quick', 'brown', 'fox', 'ate', 'some', 'spam', 'and', > 'eggs'] > > In [18]: '\t'.join(x.split()) > Out[18]: 'the\tquick\tbrown\tfox\tate\tsome\tspam\tand\teggs' > > In [19]: print '\t'.join(x.split()) > the quick brown fox ate some spam and eggs > > > HTH, > Wayne > -- Jyotsna Guleria
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor