How to create tabs in a file??

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2009
Posts: 27
Reputation: joe82 is an unknown quantity at this point 
Solved Threads: 0
joe82 joe82 is offline Offline
Light Poster

How to create tabs in a file??

 
0
  #1
Jul 6th, 2009
Hello everyone,

My file has text like:

gnl|dbSNP|rs13484118 gi|62750812|ref|NC_005111.2|NC_005111 95 20 1 0 68 87 31017559 31017578 4.4 32.3
gnl|dbSNP|rs13484118 gi|62750812|ref|NC_005111.2|NC_005111 91.67 24 2 0 63 86 35247737 35247714 4.4 32.3
gnl|dbSNP|rs13484118 gi|62750812|ref|NC_005111.2|NC_005111 91.67 24 2 0 64 87 40549054 40549031 4.4 32.3
gnl|dbSNP|rs13484118 gi|62750812|ref|NC_005111.2|NC_005111 92 24 2 0 63 86 42462636 42462659 4.4 32.3
gnl|dbSNP|rs13484118 gi|62750812|ref|NC_005111.2|NC_005111 95 20 1 0 63 82 45774066 45774085 4.4 32.3

I want to make it tab delimited...

Can anyone suggest on this??

Thanks in advance...!!
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 948
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 146
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: How to create tabs in a file??

 
0
  #2
Jul 6th, 2009
Well you can write a tab by using the escape character and t. So if you were writing a tab to a file you would go something like this:
  1. f = open("File.txt",'w')
  2. f.write("\t This will be indented one tab space!")
  3. f.close()

So the \t is the escape sequence used to indicate a tab.

Hope that helps
Last edited by Paul Thompson; Jul 6th, 2009 at 6:10 pm.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 27
Reputation: joe82 is an unknown quantity at this point 
Solved Threads: 0
joe82 joe82 is offline Offline
Light Poster

Re: How to create tabs in a file??

 
0
  #3
Jul 6th, 2009
Thanks for replying..

Can you please change my program given below to include tab ??

  1.  
  2. from __future__ import with_statement
  3.  
  4. with open ('C:\\Documents and Settings\\Desktop\\file2.txt') as fil:
  5. f = fil.readlines()
  6. result = []
  7. for line in f:
  8. line = line.split()
  9. if len(line) >= 3:
  10. i = float(line[2])
  11. if 90 <= i < 100:
  12. line = ' '.join(line)
  13. result.append(line)
  14.  
  15.  
  16.  
  17. with open('C:\\Documents and Settings\\Desktop\\j3.txt','w')as resultfile:
  18. resultfile.write('\n'.join(result))

in above code my output file is in the format:

gnl|dbSNP|rs13484118 gi|62750812|ref|NC_005111.2|NC_005111 95 20 1 0 68 87 31017559 31017578 4.4 32.3
gnl|dbSNP|rs13484118 gi|62750812|ref|NC_005111.2|NC_005111 91.67 24 2 0 63 86 35247737 35247714 4.4 32.3

I want each column to be separated by a tab:

Please help..!!

Thanks in advance,...
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 56
Reputation: baki100 is an unknown quantity at this point 
Solved Threads: 11
baki100 baki100 is offline Offline
Junior Poster in Training

Re: How to create tabs in a file??

 
0
  #4
Jul 7th, 2009
you can try this
  1. from __future__ import with_statement
  2. fil = open('C:\\Documents and Settings\\Desktop\\file2.txt','rb')
  3. resultfile = open('C:\\Documents and Settings\\Desktop\\j3.txt','wb')
  4. for line in fil:
  5. if len(line.split('|')) >= 3:
  6. i = float(line.split('|')[2])
  7. if 90 <= i < 100:
  8. resultfile.write('%s\r\n'%''.join(line.replace('|','\t')))
Last edited by baki100; Jul 7th, 2009 at 6:32 am. Reason: errors
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 27
Reputation: joe82 is an unknown quantity at this point 
Solved Threads: 0
joe82 joe82 is offline Offline
Light Poster

Re: How to create tabs in a file??

 
0
  #5
Jul 7th, 2009
Thank you for replying to the post but i do not want it to get separated by "|"

My first column is :gnl|dbSNP|rs13484116
second column: gi|62750812|ref|NC_005111.2|NC_005111
third: 100
fourth: 16
fifth: 0
sixth: 0
seventh: 300
eighth: 460
Ninth: 28912367
and tenth: 28912382 and so on..

my code:

  1.  
  2. from __future__ import with_statement
  3.  
  4. with open ('C:\\Documents and Settings\\Desktop\\file2.txt') as fil:
  5. f = fil.readlines()
  6. result = []
  7. for line in f:
  8. line = line.split()
  9. if len(line) >= 3:
  10. i = float(line[2])
  11. if 90 <= i < 100:
  12. line = ' '.join(line)
  13. result.append(line)
  14.  
  15.  
  16.  
  17. with open('C:\\Documents and Settings\\Desktop\\j3.txt','w')as resultfile:
  18. resultfile.write('\n'.join(result))

is taking input file ( which has tabs exactly as i explained above)

gnl|dbSNP|rs13484116 gi|62750812|ref|NC_005111.2|NC_005111 100 16 0 0 300 460 28912367 28912382 4.4 32.3
gnl|dbSNP|rs13484116 gi|62750812|ref|NC_005111.2|NC_005111 95 20 1 0 300 387 31017559 31017578 4.4 32.3

and result file is:
gnl|dbSNP|rs13484116 gi|62750812|ref|NC_005111.2|NC_005111 95 20 1 0 300 387 31017559 31017578 4.4 32.3
gnl|dbSNP|rs13484118 gi|62750812|ref|NC_005111.2|NC_005111 91.67 24 2 0 63 86 35247737 35247714 4.4 32.3

i just want my result file to be tab delimited as my input file so that i can put it in xl sheet and do whatever i have to ..or can parse it again for many different things for which i already have codes...

Please help me on this...

Many Thanks...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: How to create tabs in a file??

 
1
  #6
Jul 7th, 2009
You mean something like this:
  1. # data string read from a file
  2. data = """\
  3. gnl|dbSNP|rs13484118 gi|62750812|ref|NC_005111.2|NC_005111 95 20 1 0 68 87 31017559 31017578 4.4 32.3
  4. gnl|dbSNP|rs13484118 gi|62750812|ref|NC_005111.2|NC_005111 91.67 24 2 0 63 86 35247737 35247714 4.4 32.3
  5. gnl|dbSNP|rs13484118 gi|62750812|ref|NC_005111.2|NC_005111 91.67 24 2 0 64 87 40549054 40549031 4.4 32.3
  6. gnl|dbSNP|rs13484118 gi|62750812|ref|NC_005111.2|NC_005111 92 24 2 0 63 86 42462636 42462659 4.4 32.3
  7. gnl|dbSNP|rs13484118 gi|62750812|ref|NC_005111.2|NC_005111 95 20 1 0 63 82 45774066 45774085 4.4 32.3
  8. """
  9.  
  10. new_data = ""
  11. for line in data.split('\n'):
  12. # replace each space with a tab
  13. line = line.replace(" ", "\t")
  14. new_data += line + '\n'
  15.  
  16. # test only ...
  17. print(new_data)
  18.  
  19. # now write new_data string to a file
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 27
Reputation: joe82 is an unknown quantity at this point 
Solved Threads: 0
joe82 joe82 is offline Offline
Light Poster

Re: How to create tabs in a file??

 
0
  #7
Jul 7th, 2009
Thank you....
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC