[Tutor] How o convert spaces into tabs??
W W
srilyk at gmail.com
Tue Jun 2 20:22:15 CEST 2009
On Tue, Jun 2, 2009 at 12:42 PM, jyotsna guleria
<jyotsna.guleria at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090602/43d4be6b/attachment.htm>
More information about the Tutor
mailing list