943,600 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 702
  • Python RSS
Jul 6th, 2009
0

How to create tabs in a file??

Expand Post »
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...!!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
joe82 is offline Offline
27 posts
since Jul 2009
Jul 6th, 2009
0

Re: How to create tabs in a file??

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:
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 264
Solved Threads: 183
Veteran Poster
Paul Thompson is offline Offline
1,095 posts
since May 2008
Jul 6th, 2009
0

Re: How to create tabs in a file??

Thanks for replying..

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

python Syntax (Toggle Plain Text)
  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,...
Reputation Points: 10
Solved Threads: 0
Light Poster
joe82 is offline Offline
27 posts
since Jul 2009
Jul 7th, 2009
0

Re: How to create tabs in a file??

you can try this
python Syntax (Toggle Plain Text)
  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
Reputation Points: 12
Solved Threads: 14
Junior Poster in Training
baki100 is offline Offline
79 posts
since Apr 2009
Jul 7th, 2009
0

Re: How to create tabs in a file??

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:

python Syntax (Toggle Plain Text)
  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...
Reputation Points: 10
Solved Threads: 0
Light Poster
joe82 is offline Offline
27 posts
since Jul 2009
Jul 7th, 2009
1

Re: How to create tabs in a file??

You mean something like this:
python Syntax (Toggle Plain Text)
  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
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Jul 7th, 2009
0

Re: How to create tabs in a file??

Thank you....
Reputation Points: 10
Solved Threads: 0
Light Poster
joe82 is offline Offline
27 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Hello World! Computer Programming for Kids and Other Beginners
Next Thread in Python Forum Timeline: Don't you love newbie Questions ??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC