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...!!

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:

f = open("File.txt",'w')
f.write("\t This will be indented one tab space!")
f.close()

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

Hope that helps :)

Thanks for replying..

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

from __future__ import with_statement

with open ('C:\\Documents and Settings\\Desktop\\file2.txt') as fil:
    f = fil.readlines()
    result = []
    for line in f:
        line = line.split()
        if len(line) >= 3:
            i = float(line[2])
            if 90 <= i < 100:
                line = ' '.join(line)
                result.append(line)


                
with open('C:\\Documents and Settings\\Desktop\\j3.txt','w')as resultfile:
     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,...

you can try this

from __future__ import with_statement
fil = open('C:\\Documents and Settings\\Desktop\\file2.txt','rb')
resultfile  = open('C:\\Documents and Settings\\Desktop\\j3.txt','wb')
for line in fil:
    if len(line.split('|')) >= 3:
        i = float(line.split('|')[2])
        if 90 <= i < 100:
            resultfile.write('%s\r\n'%''.join(line.replace('|','\t')))

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:

from __future__ import with_statement

with open ('C:\\Documents and Settings\\Desktop\\file2.txt') as fil:
    f = fil.readlines()
    result = []
    for line in f:
        line = line.split()
        if len(line) >= 3:
            i = float(line[2])
            if 90 <= i < 100:
                line = ' '.join(line)
                result.append(line)


                
with open('C:\\Documents and Settings\\Desktop\\j3.txt','w')as resultfile:
     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...

You mean something like this:

# data string read from a file
data = """\
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
"""

new_data = ""
for line in data.split('\n'):
    # replace each space with a tab
    line = line.replace(" ", "\t")
    new_data += line + '\n'

# test only ...
print(new_data)

# now write new_data string to a file
Comments
ahh! Beat me too it :P
This question has already been answered. Start a new discussion instead.