473,398 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

How do I split a text file without stripping the character I'm splitting at?

Hi everyone, I'm new to python and would like to split a FASTA (text) file into each different gene (separated by a ">"), randomly sample a certain number of the sequences, and print the result. I have the program almost working correctly, but for some reason text.split('>') strips all of the ">"s from the file. If there's some way I can either remove this strip or add back in the ">" character that would be amazing. Here's my program so far, I've added the ">" character to the print line, but that only adds it at the beginning of the result, I want it at the beginning of all of the splits.

My Program:
Expand|Select|Wrap|Line Numbers
  1. import random
  2. fileobj = open("MyFile")
  3. ignore  = fileobj.read(1)
  4. text    = fileobj.read()
  5. records = text.split('>')
  6. NewLines = random.sample(records, 3)
  7. print ">" + '\n'.join(NewLines)  
Result:

>FLP3FBN01A85QC length=268 xy=0397_0946 region=1 run=R_2008_12_09_13_51_01_
ACAGACCACTCACATGCTGCCTCCCGTAGGAGTTTGGGCCGTGTCTCAGT CCCAATGTGG
CCGTTCACCCTCTCAGGCCGGCTACTGATCGTCGCCTTGGTAGGCCGTTA CCCTACCAAC
AAGCTAATCAGACGCGGAGCCATCTTACACCACCTCAGTTTTTCACACCG GACCATGCGG
TCCTGTGCGCTTATGCGGTATTAGCACCTATTTCTAAGTGTTATCCCCCT GTGTAAGGCA
GGTCCTCCACGCGTTACTCACCCGTCCG

FLP3FBN01DH3NR length=257 xy=1319_0885 region=1 run=R_2008_12_09_13_51_01_
ACAGACCACTCACATGCTGCCTCCCGTAGGAGTCTGGGCCGTGTCTCAGT CCCAATGTGG
CCGGTCACCCTCTCAGGTCGGCTACTGATCGTCGGCTTGGTGAGCCGTTA CCTCACCAAC
TACCTAATCAGACGCGGGTCCATCTTGCACCACCGGAGTTTTTCACACTG TCCCATGCAG
GACCGTGCGCTTATGCGGTATTGCACCTATTTCTAAGTGTTATCCCCCAG TGCAAGGCAG
GTTACCCACGCGTTACT

FLP3FBN01D0219 length=268 xy=1535_1839 region=1 run=R_2008_12_09_13_51_01_
ACAGACCACTCACATGCTGCCTCCCGTAGGAGTTTGGGCCGTGTCTCAGT CCCAATGTGG
CCGTCCACCCTCTCAGGCCGGCTACTGATCGTCGCCTTGGTGGGCCTTTA CCCCGCCAAC
CAGCTAATCAGACGCGGGTCCATCTTGCACCACCGGAGTTTTTCACACTG TCCCATGCAG
GACCGTGCGCTTATGCGGTATTAGCACCTATTTCTAAGTGTTATCCCCCA GTGCAAGGCA
GGTTACCCACGCGTTACTCACCCGTCCG

So, basically all I need is the ">" at the beginning of each of the three paragraphs.

Thanks so much in advance!
Nov 5 '10 #1

✓ answered by bvdet

In your case it would be:
Expand|Select|Wrap|Line Numbers
  1. NewLines = [">%s" % (s) for s in random.sample(records, 3)]
OR:
Expand|Select|Wrap|Line Numbers
  1. records = [">%s" % (s) for s in text.split('>')]

4 2895
bvdet
2,851 Expert Mod 2GB
Use a list comprehension to add the ">" character to each record. Example:
Expand|Select|Wrap|Line Numbers
  1. >>> records = ["123", "456", "789"]
  2. >>> new_records = [">%s" % (s) for s in records]
  3. >>> new_records
  4. ['>123', '>456', '>789']
  5. >>> 
Nov 5 '10 #2
Would I have to convert my list to a string for this? Also, for "records = ["123", "456", "789"]" how would I get python to automatically fill in for the "123", "456", and "789" in your example? The text file I'm using is hundreds of thousands of characters long, so I can't possibly do this manually.
Nov 5 '10 #3
bvdet
2,851 Expert Mod 2GB
In your case it would be:
Expand|Select|Wrap|Line Numbers
  1. NewLines = [">%s" % (s) for s in random.sample(records, 3)]
OR:
Expand|Select|Wrap|Line Numbers
  1. records = [">%s" % (s) for s in text.split('>')]
Nov 5 '10 #4
Thanks! That works perfectly!
Nov 5 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Rune Johansen | last post by:
Hi, I'm sorry if these questions are trivial, but I've searched the net and haven't had any luck finding the information I need. I need to perform some regular expression search and replace on...
4
by: qwweeeit | last post by:
The standard split() can use only one delimiter. To split a text file into words you need multiple delimiters like blank, punctuation, math signs (+-*/), parenteses and so on. I didn't...
2
by: Jeffrey | last post by:
What classes/methods can I use to do the following? 1. Open a text file (less than 50k in size) 2. Replace some text in it. 3. Close the text file. I don't want to have to create a new file...
4
by: Phil | last post by:
Hi, Is there a 'shorter' way to find a string within a text file without 'testing' each character or using the LineInput function. Does the StreamReader have any search facility? Thanks, ...
0
by: jpauthement | last post by:
I have an application which searches through a comma-delimited text file which looks similar to this: "012233010","PAMIC 6X8","FA","0.000","0.000" "012233011","PAMIC 8X8","FA","1.000","0.000" ...
2
by: ownowl | last post by:
Hello beginer under python, I have a problem to get lines in a text file. lines have inside the \n (x0A) char, and le readline method split the line at this char too (not only at x0Dx0A). for...
2
by: RSH | last post by:
Hi, I have a situation where I am querying SQL Server and bringing back a Dataset. Is there a way to dump the datatable to a delimited text file without iteration through the datatable? The...
3
by: Tom | last post by:
I don't want to re-invent the wheel and am looking for a simple implementation of a text viewer or RichTextBox in read only mode that allows rapid file positioning within large data files without...
4
by: mvvdsteen | last post by:
Hello all, I'm quite new to c++. I made a small program that will help me analyse wind tunnel data. But now I want this program to write to a text file. This works just fine, except it discards...
5
by: Myxamatosis | last post by:
Currently I need to read data from a file, such like this Measurements for Lansing, Michigan during April, 2000 63 32 0.00 54 43 0.10 59 39 0.00 46 24 0.00 52 20 0.00 54 30 0.00
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.