Multiple strings replacement from dictionary - python
I'm going to create script which will take the key and value from the dictionary and use it for replacement in set of files.
I need replace "foo" to "foo1" if "target_value" found in the file. There are many different foo's. So, I guess dictionary will be suitable for that.
I started from the simple things:
with fileinput.FileInput(filelist, inplace=True) as file:
for line in file:
if line.find('target_value'):
print(line.replace("foo", "foo1"), end='')
For some reason this script just ignore line.find and replace everything with last line of code.
Could you help?
Python's find command returns -1 if the value is not found, so you need something like:
with fileinput.FileInput(filelist, inplace=True) as file:
for line in file:
if line.find('target_value') > -1:
line = line.replace("foo", "foo1")
print(line, end='')
The trouble with str.find is that it returns the index at which "target_value" occurs in line, so any integer from 0 to len(line)-len(target_value)-1. That is, unless, "target_value" doesn't exist in line; then str.find returns -1 but the value of bool(-1) is True. In fact, the only time line.find('target_value') is False is when "target_value" is the first part of line.
There are a couple options:
with fileinput.FileInput(filelist, inplace=True) as file:
for line in file:
if line.find('target_value') != -1:
print(line.replace("foo", "foo1"), end='')
Or:
with fileinput.FileInput(filelist, inplace=True) as file:
for line in file:
if 'target_value' in line:
print(line.replace("foo", "foo1"), end='')
The second option is more readable and tends to perform better when line is long and "target_value" doesn't occur in the beginning of line.
>>> timeit('"target_value" in s', setup='s = "".join("foobar baz" for _ in range(100))+"target_value"+ "".join("foobar baz" for _ in range(100))')
0.20444475099975534
>>> timeit('s.find("target_value") != -1', setup='s = "".join("foobar baz" for _ in range(100))+"target_value"+ "".join("foobar baz" for _ in range(100))')
0.30517548999978317
Instead of .find(), you can use if 'target_value' in line: which is more expressive and does not involve return value conventions.
If you have multiple target keywords (and multiple replacements per target), you could build your dictionary in like this
replacements = { 'target_value1': [('foo','foo1'), ('bar','bar1')],
'target_value2': [('oof','oof'), ('rab','rab2')],
'target_value3': [('foobar','foosy'),('barfoo','barry')]}
Then find which target value(s) are present and perform the corresponding replacements:
with open(fileName,'r') as f:
content = f.read()
for target,maps in replacements.items(): # go through target keywords
if target not in content: continue # check target (whole file)
for fromString,toString in maps: # perform replacements for target
content = content.replace(fromString,toString)
# you probably want to save the new content at this point
with open(fileName,'w') as f:
f.write(content)
Note that, in this example, I assumed that the target keyword flags the whole file (not each line). If the target keyword is specific to each line, you will need to break down the content into lines and place the logic inside a loop on lines to perform the replacements on a line by line basis
You don't actually need the replacement data to be a dictionary (this second example with line level single target replacements):
replacements = [ ['target_value1',('foo','foo1'), ('bar','bar1')],
['target_value2',('oof','oof'), ('rab','rab2')],
['target_value3',('foobar','foosy'),('barfoo','barry')]]
with open(fileName,'r') as f:
lines = f.read().split("\n")
for i,line in enumerate(lines): # for each line
for target,*maps in replacements: # go through target keywords
if target not in line: continue # check target (line level)
for fromString,toString in maps: # perform replacements on line
lines[i] = lines[i].replace(fromString,toString)
break # only process one keyword per line
# to save changes
with open(fileName,'w') as f:
f.write("\n".join(lines))
Related
Replacement for isAlpha() to include underscores?
I am processing data using Python3 and I need to read a results file that looks like this: ENERGY_BOUNDS 1.964033E+07 1.733253E+07 1.491825E+07 1.384031E+07 1.161834E+07 1.000000E+07 8.187308E+06 6.703200E+06 6.065307E+06 5.488116E+06 4.493290E+06 3.678794E+06 3.011942E+06 2.465970E+06 2.231302E+06 2.018965E+06 GAMMA_INTERFACE 0 EIGENVALUE 1.219034E+00 I want to search the file for a specific identifier (in this case ENERGY_BOUNDS), begin reading the numeric values after this identifier but not the identifier itself, and stop when I reach the next identifier. However, my problem is that I was using isAlpha to find the next identifier, and some of them contain underscores. Here is my code: def read_data_from_file(file_name, identifier): with open(file_name, 'r') as read_obj: list_of_results = [] # Read all lines in the file one by one for line in read_obj: # For each line, check if line contains the string if identifier in line: # If yes, read the next line nextValue = next(read_obj) while(not nextValue.strip().isalpha()): # Keep on reading until next identifier appears list_of_results.extend(nextValue.split()) nextValue = next(read_obj) return(list_of_results) I think I need to use regex, but I am stuck regarding how to phrase it. Any help would be much appreciated!
take = False with open('path/to/input') as infile: for line in input: if line.strip() == "ENERGY_BOUNDS": take = True continue # we don't actually want this line if all(char.isalpha() or char=="_" for char in line.strip()): # we've hit the next section take = False if take: print(line) # or whatever else you want to do with this line
Here's an option for you. Just iterate over the file until you hit the identifier. Then iterate over it in another for loop until the next identifier causes a ValueError. def read_data_from_file(file_name, identifier): with open(file_name, 'r') as f: list_of_results = [] for line in f: if identifier in line: break for line in f: try: list_of_results.extend(map(float, line.split())) except ValueError: break return list_of_results
You can use this regex: ^[A-Z]+(?:_[A-Z]+)*$ Additionally, you can modify the regex to match strings of custom length, like this: ^[A-Z]{2,10}+(?:_[A-Z]+)*$, where {2, 10} is {MIN, MAX} length: You can find this demo here: https://regex101.com/r/9jESAH/35 See this answer for more details.
Here is a simple function to verify a string has alpha, uppercase and lowercase and underscore: RE_PY_VAR_NAME="^[a-zA-Z_]+$" def isAlphaUscore(s:str) -> bool: assert not s is None, "s cannot be None" return re.search(RE_PY_VAR_NAME, s)
Python: Access "field" in line
I have the following .txt-File (modified bash emboss-dreg report, the original report has seqtable format): Start End Strand Pattern Sequence 43392 43420 + regex:[T][G][A][TC][C][CTG]\D{15,17}[CA][G][T][AT][AT][CTA] TGATCGCACGCCGAATGGAAACACGTTTT 52037 52064 + regex:[T][G][A][TC][C][CTG]\D{15,17}[CA][G][T][AT][AT][CTA] TGACCCTGCTTGGCGATCCCGGCGTTTC 188334 188360 + regex:[T][G][A][TC][C][CTG]\D{15,17}[CA][G][T][AT][AT][CTA] TGATCGCGCAACTGCAGCGGGAGTTAC I would like to access the elements under "sequence" only, to compare them with some variables and delete the whole lines, if the comparison does not give the desired result (using Levenshtein distance for comparison). But I can't even get started .... :( I am searching for something like the linux -f option, to directly get to the right "field" in the line to do my comparison. I came across re.split: with open(textFile) as f: for line in f: cleaned=re.split(r'\t',line) print(cleaned) which results in: [' Start End Strand Pattern Sequence\n'] ['\n'] [' 43392 43420 + regex:[T][G][A][TC][C][CTG]\\D{15,17}[CA][G][T][AT][AT][CTA] TGATCGCACGCCGAATGGAAACACGTTTT\n'] ['\n'] [' 52037 52064 + regex:[T][G][A][TC][C][CTG]\\D{15,17}[CA][G][T][AT][AT][CTA] TGACCCTGCTTGGCGATCCCGGCGTTTC\n'] ['\n'] [' 188334 188360 + regex:[T][G][A][TC][C][CTG]\\D{15,17}[CA][G][T][AT][AT][CTA] TGATCGCGCAACTGCAGCGGGAGTTAC\n'] ['\n'] That is the closest I got to "split my lines into elements". I feel like totally going the wrong way, but searching Stack Overflow and google did not result in anything :( I have never worked with seqtable-format before, so I tried to deal with it as .txt Maybe, there is another approach better for dealing with it? Python is the main language I am learning, I am not so firm in Bash, but bash-answers for dealing with the issue would be ok for me, too. I am thankful for any hint/link/help :)
The format itself seems to be using multiple lines as delimiters while your r'\t' is not doing anything (you're instructing Python to split on a literal \t). Also, based on what you've pasted the data is not using a tab delimiter anyway, but a random number of whitespaces to pad the table. To address both, you can read the file, treat the first line as a header (if you need it), then read the rest line by line, strip the trailing\leading whitespace, check if there is any data there and if there is - further split it on whitespace to get to your line elements: with open("your_data", "r") as f: header = f.readline().split() # read the first line as a header for line in f: # read the rest of the file line-by-line line = line.strip() # first clear out the whitespace if line: # check if there is any content left or is it an empty line elements = line.split() # split the data on whitespace to get your elements print(elements[-1]) # print the last element TGATCGCACGCCGAATGGAAACACGTTTT TGACCCTGCTTGGCGATCCCGGCGTTTC TGATCGCGCAACTGCAGCGGGAGTTAC As a bonus, since you have the header, you can turn it into a map and then use 'proxied' named access to get the element you're looking for so you don't need to worry about the element position: with open("your_data", "r") as f: # read the header and turn it into a value:index map header = {v: i for i, v in enumerate(f.readline().split())} for line in f: # read the rest of the file line-by-line line = line.strip() # first clear out the whitespace if line: # check if there is any content left or is it an empty line elements = line.split() print(elements[header["Sequence"]]) # print the Sequence element You can also use a header map to turn your rows into dict structures for even easier access. UPDATE: Here's how to create a header map and then use it to build a dict out of your lines: with open("your_data", "r") as f: # read the header and turn it into an index:value map header = {i: v for i, v in enumerate(f.readline().split())} for line in f: # read the rest of the file line-by-line line = line.strip() # first clear out the whitespace if line: # check if there is any content left or is it an empty line # split the line, iterate over it and use the header map to create a dict row = {header[i]: v for i, v in enumerate(line.split())} print(row["Sequence"]) # ... or you can append it to a list for later use As for how to 'delete' lines that you don't want for some reason, you'll have to create a temporary file, loop through your original file, compare your values, write the ones that you want to keep into the temporary file, delete the original file and finally rename the temporary file to match your original file, something like: import shutil from tempfile import NamedTemporaryFile SOURCE_FILE = "your_data" # path to the original file to process def compare_func(seq): # a simple comparison function for our sequence return not seq.endswith("TC") # use Levenshtein distance or whatever you want instead # open a temporary file for writing and our source file for reading with NamedTemporaryFile(mode="w", delete=False) as t, open(SOURCE_FILE, "r") as f: header_line = f.readline() # read the header t.write(header_line) # write the header immediately to the temporary file header = {v: i for i, v in enumerate(header_line.split())} # create a header map last_line = "" # a var to store the whitespace to keep the same format for line in f: # read the rest of the file line-by-line row = line.strip() # first clear out the whitespace if row: # check if there is any content left or is it an empty line elements = row.split() # split the row into elements # now lets call our comparison function if compare_func(elements[header["Sequence"]]): # keep the line if True t.write(last_line) # write down the last whitespace to the temporary file t.write(line) # write down the current line to the temporary file else: last_line = line # store the whitespace for later use shutil.move(t.name, SOURCE_FILE) # finally, overwrite the source with the temporary file This will produce the same file sans the second row from your example since its sequence ends in a TC and our comp_function() returns False in that case. For a bit less complexity, instead of using temporary files you can load your whole source file into the working memory and then just overwrite it, but that would work only for files that can fit your working memory while the above approach can work with files as large as your free storage space.
How to convert a list into float for using the '.join' function?
I have to compress a file into a list of words and list of positions to recreate the original file. My program should also be able to take a compressed file and recreate the full text, including punctuation and capitalization, of the original file. I have everything correct apart from the recreation, using the map function my program can't convert my list of positions into floats because of the '[' as it is a list. My code is: text = open("speech.txt") CharactersUnique = [] ListOfPositions = [] DownLine = False while True: line = text.readline() if not line: break TwoList = line.split() for word in TwoList: if word not in CharactersUnique: CharactersUnique.append(word) ListOfPositions.append(CharactersUnique.index(word)) if not DownLine: CharactersUnique.append("\n") DownLine = True ListOfPositions.append(CharactersUnique.index("\n")) w = open("List_WordsPos.txt", "w") for c in CharactersUnique: w.write(c) w.close() x = open("List_WordsPos.txt", "a") x.write(str(ListOfPositions)) x.close() with open("List_WordsPos.txt", "r") as f: NewWordsUnique = f.readline() f.close() h = open("List_WordsPos.txt", "r") lines = h.readlines() NewListOfPositions = lines[1] NewListOfPositions = map(float, NewListOfPositions) print("Recreated Text:\n") recreation = " " .join(NewWordsUnique[pos] for pos in (NewListOfPositions)) print(recreation) The error I get is: Task 3 Code.py", line 42, in <genexpr> recreation = " " .join(NewWordsUnique[pos] for pos in (NewListOfPositions)) ValueError: could not convert string to float: '[' I am using Python IDLE 3.5 (32-bit). Does anyone have any ideas on how to fix this?
Why do you want to turn the position values in the list into floats, since they list indices, and those must be integer? I suspected this might be an instance of what is called the XY Problem. I also found your code difficult to understand because you haven't followed the PEP 8 - Style Guide for Python Code. In particular, with how many (although not all) of the variable names are CamelCased, which according to the guidelines, should should be reserved for the class names. In addition some of your variables had misleading names, like CharactersUnique, which actually [mostly] contained unique words. So, one of the first things I did was transform all the CamelCased variables into lowercase underscore-separated words, like camel_case. In several instances I also gave them better names to reflect their actual contents or role: For example: CharactersUnique became unique_words. The next step was to improve the handling of files by using Python's with statement to ensure they all would be closed automatically at the end of the block. In other cases I consolidated multiple file open() calls into one. After all that I had it almost working, but that's when I discovered a problem with the approach of treating newline "\n" characters as separate words of the input text file. This caused a problem when the file was being recreated by the expression: " ".join(NewWordsUnique[pos] for pos in (NewListOfPositions)) because it adds one space before and after every "\n" character encountered that aren't there in the original file. To workaround that, I ended up writing out the for loop that recreates the file instead of using a list comprehension, because doing so allows the newline "words" could be handled properly. At any rate, here's the resulting rewritten (and working) code: input_filename = "speech.txt" compressed_filename = "List_WordsPos.txt" # Two lists to represent contents of input file. unique_words = ["\n"] # preload with newline "word" word_positions = [] with open(input_filename, "r") as input_file: for line in input_file: for word in line.split(): if word not in unique_words: unique_words.append(word) word_positions.append(unique_words.index(word)) word_positions.append(unique_words.index("\n")) # add newline at end of each line # Write representations of the two data-structures to compressed file. with open(compressed_filename, "w") as compr_file: words_repr = " ".join(repr(word) for word in unique_words) compr_file.write(words_repr + "\n") positions_repr = " ".join(repr(posn) for posn in word_positions) compr_file.write(positions_repr + "\n") def strip_quotes(word): """Strip the first and last characters from the string (assumed to be quotes).""" tmp = word[1:-1] return tmp if tmp != "\\n" else "\n" # newline "words" are special case # Recreate input file from data in compressed file. with open(compressed_filename, "r") as compr_file: line = compr_file.readline() new_unique_words = list(map(strip_quotes, line.split())) line = compr_file.readline() new_word_positions = map(int, line.split()) # using int, not float here words = [] lines = [] for posn in new_word_positions: word = new_unique_words[posn] if word != "\n": words.append(word) else: lines.append(" ".join(words)) words = [] print("Recreated Text:\n") recreation = "\n".join(lines) print(recreation) I created my own speech.txt test file from the first paragraph of your question and ran the script on it with these results: Recreated Text: I have to compress a file into a list of words and list of positions to recreate the original file. My program should also be able to take a compressed file and recreate the full text, including punctuation and capitalization, of the original file. I have everything correct apart from the recreation, using the map function my program can't convert my list of positions into floats because of the '[' as it is a list.
Per your question in the comments: You will want to split the input on spaces. You will also likely want to use different data structures. # we'll map the words to a list of positions all_words = {} with open("speech.text") as f: data = f.read() # since we need to be able to re-create the file, we'll want # line breaks lines = data.split("\n") for i, line in enumerate(lines): words = line.split(" ") for j, word in enumerate(words): if word in all_words: all_words[word].append((i, j)) # line and pos else: all_words[word] = [(i, j)] Note that this does not yield maximum compression as foo and foo. count as separate words. If you want more compression, you'll have to go character by character. Hopefully now you can use a similar approach to do so if desired.
Splitting lines in python based on some character
Input: !,A,56281,12/12/19,19:34:12,000.0,0,37N22.714,121W55.576,+0013!,A,56281,12/1 2/19,19:34:13,000.0,0,37N22.714,121W55.576,+0013!,A,56281,12/12/19,19:34:14,000. 0,0,37N22.714,121W55.576,+0013!,A,56281,12/12/19,19:34:15,000.0,0,37N22.714,121W 55.576,+0013!,A,56281,12/12/19,19:34:16,000.0,0,37N22.714,121W55.576,+0013!,A,56 281,12/12/19,19:34:17,000.0,0,37N22.714,121W55.576,+0013!,A,56281,12/12/19,19:34 :18,000.0,0,37N22.714,121W55.576,+0013!,A,56281,12/12/19,19:34:19,000.0,0,37N22. Output: !,A,56281,12/12/19,19:34:12,000.0,0,37N22.714,121W55.576,+0013 !,A,56281,12/12/19,19:34:13,000.0,0,37N22.714,121W55.576,+0013 !,A,56281,12/12/19,19:34:14,000.0,0,37N22.714,121W55.576,+0013 !,A,56281,12/12/19,19:34:15,000.0,0,37N22.714,121W55.576,+0013 !,A,56281,12/12/19,19:34:16,000.0,0,37N22.714,121W55.576,+0013 !,A,56281,12/12/19,19:34:17,000.0,0,37N22.714,121W55.576,+0013 !,A,56281,12/12/19,19:34:18,000.0,0,37N22.714,121W55.576,+0013 !,A,56281,12/12/19,19:34:19,000.0,0,37N22. '!' is the starting character and +0013 should be the ending of each line (if present). Problem which I am getting: Output is like : !,A,56281,12/12/19,19:34:12,000.0,0,37N22.714,121W55.576,+0013 !,A,56281,12/1 2/19,19:34:13,000.0,0,37N22.714,121W55.576,+0013 !,A,56281,12/12/19,19:34:14,000. 0,0,37N22.714,121W55.576,+0013 !,A,56281,12/12/19,19:34:15,000.0,0,37N22.714,121W Any help would be highly appreciated...!!! My code: file_open= open('sample.txt','r') file_read= file_open.read() file_open2= open('output.txt','w+') counter =0 for i in file_read: if '!' in i: if counter == 1: file_open2.write('\n') counter= counter -1 counter= counter +1 file_open2.write(i)
You can try something like this: with open("abc.txt") as f: data=f.read().replace("\r\n","") #replace the newlines with "" #the newline can be "\n" in your system instead of "\r\n" ans=filter(None,data.split("!")) #split the data at '!', then filter out empty lines for x in ans: print "!"+x #or write to some other file .....: !,A,56281,12/12/19,19:34:12,000.0,0,37N22.714,121W55.576,+0013 !,A,56281,12/12/19,19:34:13,000.0,0,37N22.714,121W55.576,+0013 !,A,56281,12/12/19,19:34:14,000.0,0,37N22.714,121W55.576,+0013 !,A,56281,12/12/19,19:34:15,000.0,0,37N22.714,121W55.576,+0013 !,A,56281,12/12/19,19:34:16,000.0,0,37N22.714,121W55.576,+0013 !,A,56281,12/12/19,19:34:17,000.0,0,37N22.714,121W55.576,+0013 !,A,56281,12/12/19,19:34:18,000.0,0,37N22.714,121W55.576,+0013 !,A,56281,12/12/19,19:34:19,000.0,0,37N22.
Could you just use str.split? lines = file_read.split('!') Now lines is a list which holds the split data. This is almost the lines you want to write -- The only difference is that they don't have trailing newlines and they don't have '!' at the start. We can put those in easily with string formatting -- e.g. '!{0}\n'.format(line). Then we can put that whole thing in a generator expression which we'll pass to file.writelines to put the data in a new file: file_open2.writelines('!{0}\n'.format(line) for line in lines) You might need: file_open2.writelines('!{0}\n'.format(line.replace('\n','')) for line in lines) if you find that you're getting more newlines than you wanted in the output. A few other points, when opening files, it's nice to use a context manager -- This makes sure that the file is closed properly: with open('inputfile') as fin: lines = fin.read() with open('outputfile','w') as fout: fout.writelines('!{0}\n'.format(line.replace('\n','')) for line in lines)
Another option, using replace instead of split, since you know the starting and ending characters of each line: In [14]: data = """!,A,56281,12/12/19,19:34:12,000.0,0,37N22.714,121W55.576,+0013!,A,56281,12/1 2/19,19:34:13,000.0,0,37N22.714,121W55.576,+0013!,A,56281,12/12/19,19:34:14,000. 0,0,37N22.714,121W55.576,+0013!,A,56281,12/12/19,19:34:15,000.0,0,37N22.714,121W 55.576,+0013!,A,56281,12/12/19,19:34:16,000.0,0,37N22.714,121W55.576,+0013!,A,56 281,12/12/19,19:34:17,000.0,0,37N22.714,121W55.576,+0013!,A,56281,12/12/19,19:34 :18,000.0,0,37N22.714,121W55.576,+0013!,A,56281,12/12/19,19:34:19,000.0,0,37N22.""".replace('\n', '') In [15]: print data.replace('+0013!', "+0013\n!") !,A,56281,12/12/19,19:34:12,000.0,0,37N22.714,121W55.576,+0013 !,A,56281,12/12/19,19:34:13,000.0,0,37N22.714,121W55.576,+0013 !,A,56281,12/12/19,19:34:14,000.0,0,37N22.714,121W55.576,+0013 !,A,56281,12/12/19,19:34:15,000.0,0,37N22.714,121W55.576,+0013 !,A,56281,12/12/19,19:34:16,000.0,0,37N22.714,121W55.576,+0013 !,A,56281,12/12/19,19:34:17,000.0,0,37N22.714,121W55.576,+0013 !,A,56281,12/12/19,19:34:18,000.0,0,37N22.714,121W55.576,+0013 !,A,56281,12/12/19,19:34:19,000.0,0,37N22.
Just for some variance, here is a regular expression answer: import re outputFile = open('output.txt', 'w+') with open('sample.txt', 'r') as f: for line in re.findall("!.+?(?=!|$)", f.read(), re.DOTALL): outputFile.write(line.replace("\n", "") + '\n') outputFile.close() It will open the output file, get the contents of the input file, and loop through all the matches using the regular expression !.+?(?=!|$) with the re.DOTALL flag. The regular expression explanation & what it matches can be found here: http://regex101.com/r/aK6aV4 After we have a match, we strip out the new lines from the match, and write it to the file.
Let's try to add a \n before every "!"; then let python splitlines :-) : file_read.replace("!", "!\n").splitlines()
I will actually implement as a generator so that you can work on the data stream rather than the entire content of the file. This will be quite memory friendly if working with huge files >>> def split_on_stream(it,sep="!"): prev = "" for line in it: line = (prev + line.strip()).split(sep) for parts in line[:-1]: yield parts prev = line[-1] yield prev >>> with open("test.txt") as fin: for parts in split_on_stream(fin): print parts ,A,56281,12/12/19,19:34:12,000.0,0,37N22.714,121W55.576,+0013 ,A,56281,12/12/19,19:34:13,000.0,0,37N22.714,121W55.576,+0013 ,A,56281,12/12/19,19:34:14,000.0,0,37N22.714,121W55.576,+0013 ,A,56281,12/12/19,19:34:15,000.0,0,37N22.714,121W55.576,+0013 ,A,56281,12/12/19,19:34:16,000.0,0,37N22.714,121W55.576,+0013 ,A,56281,12/12/19,19:34:17,000.0,0,37N22.714,121W55.576,+0013 ,A,56281,12/12/19,19:34:18,000.0,0,37N22.714,121W55.576,+0013 ,A,56281,12/12/19,19:34:19,000.0,0,37N22.
How can I use readline() to begin from the second line?
I'm writing a short program in Python that will read a FASTA file which is usually in this format: >gi|253795547|ref|NC_012960.1| Candidatus Hodgkinia cicadicola Dsem chromosome, 52 lines GACGGCTTGTTTGCGTGCGACGAGTTTAGGATTGCTCTTTTGCTAAGCTTGGGGGTTGCGCCCAAAGTGA TTAGATTTTCCGACAGCGTACGGCGCGCGCTGCTGAACGTGGCCACTGAGCTTACACCTCATTTCAGCGC TCGCTTGCTGGCGAAGCTGGCAGCAGCTTGTTAATGCTAGTGTTGGGCTCGCCGAAAGCTGGCAGGTCGA I've created another program that reads the first line(aka header) of this FASTA file and now I want this second program to start reading and printing beginning from the sequence. How would I do that? so far i have this: FASTA = open("test.txt", "r") def readSeq(FASTA): """returns the DNA sequence of a FASTA file""" for line in FASTA: line = line.strip() print line readSeq(FASTA) Thanks guys -Noob
def readSeq(FASTA): """returns the DNA sequence of a FASTA file""" _unused = FASTA.next() # skip heading record for line in FASTA: line = line.strip() print line Read the docs on file.next() to see why you should be wary of mixing file.readline() with for line in file:
you should show your script. To read from second line, something like this f=open("file") f.readline() for line in f: print line f.close()
You might be interested in checking BioPythons handling of Fasta files (source). def FastaIterator(handle, alphabet = single_letter_alphabet, title2ids = None): """Generator function to iterate over Fasta records (as SeqRecord objects). handle - input file alphabet - optional alphabet title2ids - A function that, when given the title of the FASTA file (without the beginning >), will return the id, name and description (in that order) for the record as a tuple of strings. If this is not given, then the entire title line will be used as the description, and the first word as the id and name. Note that use of title2ids matches that of Bio.Fasta.SequenceParser but the defaults are slightly different. """ #Skip any text before the first record (e.g. blank lines, comments) while True: line = handle.readline() if line == "" : return #Premature end of file, or just empty? if line[0] == ">": break while True: if line[0]!=">": raise ValueError("Records in Fasta files should start with '>' character") if title2ids: id, name, descr = title2ids(line[1:].rstrip()) else: descr = line[1:].rstrip() id = descr.split()[0] name = id lines = [] line = handle.readline() while True: if not line : break if line[0] == ">": break #Remove trailing whitespace, and any internal spaces #(and any embedded \r which are possible in mangled files #when not opened in universal read lines mode) lines.append(line.rstrip().replace(" ","").replace("\r","")) line = handle.readline() #Return the record and then continue... yield SeqRecord(Seq("".join(lines), alphabet), id = id, name = name, description = descr) if not line : return #StopIteration assert False, "Should not reach this line"
good to see another bioinformatician :) just include an if clause within your for loop above the line.strip() call def readSeq(FASTA): for line in FASTA: if line.startswith('>'): continue line = line.strip() print(line)
A pythonic and simple way to do this would be slice notation. >>> f = open('filename') >>> lines = f.readlines() >>> lines[1:] ['TTAGATTTTCCGACAGCGTACGGCGCGCGCTGCTGAACGTGGCCACTGAGCTTACACCTCATTTCAGCGC\n', 'TCGCTTGCTGGCGAAGCTGGCAGCAGCTTGTTAATGCTAGTG TTGGGCTCGCCGAAAGCTGGCAGGTCGA'] That says "give me all elements of lines, from the second (index 1) to the end. Other general uses of slice notation: s[i:j] slice of s from i to j s[i:j:k] slice of s from i to j with step k (k can be negative to go backward) Either i or j can be omitted (to imply the beginning or the end), and j can be negative to indicate a number of elements from the end. s[:-1] All but the last element. Edit in response to gnibbler's comment: If the file is truly massive you can use iterator slicing to get the same effect while making sure you don't get the whole thing in memory. import itertools f = open("filename") #start at the second line, don't stop, stride by one for line in itertools.islice(f, 1, None, 1): print line "islicing" doesn't have the nice syntax or extra features of regular slicing, but it's a nice approach to remember.