| | |
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:
Solved Threads: 0
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...!!
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:
So the \t is the escape sequence used to indicate a tab.
Hope that helps
python Syntax (Toggle Plain Text)
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
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
Check out my Site | and join us on IRC | Python Specific IRC
•
•
Join Date: Jul 2009
Posts: 27
Reputation:
Solved Threads: 0
Thanks for replying..
Can you please change my program given below to include tab ??
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,...
Can you please change my program given below to include tab ??
python Syntax (Toggle Plain Text)
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,...
•
•
Join Date: Apr 2009
Posts: 56
Reputation:
Solved Threads: 11
you can try this
python Syntax (Toggle Plain Text)
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')))
Last edited by baki100; Jul 7th, 2009 at 6:32 am. Reason: errors
•
•
Join Date: Jul 2009
Posts: 27
Reputation:
Solved Threads: 0
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:
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...
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)
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:
python Syntax (Toggle Plain Text)
# 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
drink her pretty
![]() |
Similar Threads
- Unable to create an excel file in VB.net (VB.NET)
- I want to Create PDF file from Delphi,Pls help me (Pascal and Delphi)
- How do you create a text file? (C)
- how to create swf file in vb6 from bmp imgs in directory and wav file (Visual Basic 4 / 5 / 6)
- Create .wav file with C# (C#)
- How to create a JAR file ? (Java)
- Create log file in Visual C++.NET (C++)
Other Threads in the Python Forum
- Previous Thread: Help with A login program
- Next Thread: Don't you love newbie Questions ??
| Thread Tools | Search this Thread |
Tag cloud for Python
ansi assignment avogadro backend beginner binary bluetooth character cmd code copy customdialog data decimals dictionary drive dynamic error examples excel exe file float format ftp function gnu graphics gui heads homework http ideas import input java leftmouse line linux list lists logging loop module mouse newb number numbers output parsing path pointer port prime program programming progressbar projects push py2exe pygame pyglet pyqt python random recursion recursive refresh schedule scrolledtext sqlite ssh statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial ubuntu unicode update urllib urllib2 variable wikipedia windows write wxpython xlib






