Python-使用seek写入文件
我是Python的初学者,正在尝试各种方法来完成简单的任务,即反向补充DNA或RNA序列以学习一些字符串函数等。我的最新方法几乎可以工作,但对于一个我找不到答案的小问题,可能是因为我正在使用的某些东西我没有正确理解。 我的函数被设计成写一个空白文件(这很有效!),然后打开一个包含序列的文件,一次循环一个字符,将其反向补码写入新文件。代码如下:Python-使用seek写入文件,python,string,seek,Python,String,Seek,我是Python的初学者,正在尝试各种方法来完成简单的任务,即反向补充DNA或RNA序列以学习一些字符串函数等。我的最新方法几乎可以工作,但对于一个我找不到答案的小问题,可能是因为我正在使用的某些东西我没有正确理解。 我的函数被设计成写一个空白文件(这很有效!),然后打开一个包含序列的文件,一次循环一个字符,将其反向补码写入新文件。代码如下: def func_rev_seq(in_path,out_path): """ Read file one character at a time and
def func_rev_seq(in_path,out_path):
"""
Read file one character at a time and retrun the reverse complement of each nucleotide to a new file
"""
# Write a blank file (out_path)
fb = open(out_path,"w")
fb.write("")
fb.close()
# Dictionary where the key is the nucleotide and the value is its reverse complement
base = {"A":"T", "C":"G", "G":"C", "T":"A", "a":"t", "c":"g", "g":"c", "t":"a", "k":"m", "m":"k", "y":"r", "r":"y", "b":"v", "v":"b", "d":"h", "h":"d", "K":"M", "M":"K", "Y":"R", "R":"Y", "B":"V", "V":"B", "D":"H", "H":"D", "U":"A", "u":"a"}
# Open the source file (in_path) as fi
fi=open(in_path,"r")
i = fi.read(1)
# Loop through the source file one character at a time and write the reverse complement to the output file
while i != "":
i = fi.read(1)
if i in base:
b = base[i]
else:
b = i
with open(out_path, 'r+') as fo:
body = fo.read()
fo.seek(0, 0)
fo.write(b + body)
fi.close()
fo.close()
问题是,当我运行函数时,输出文件中的字符串首先被一个字符截断,然后在我不想要的空行下面。
据我所知,带有(0,0)的seek函数应该引用文件的开头,但我可能误解了。
非常感谢您的帮助,谢谢 当您放置
i=fi.read(1)
时,i
等于文件中的第一个字符,但是在while
循环的开头,您使用相同的语句将第二个字符分配给i
,而不使用第一个字符执行任何操作。如果希望循环文件中的每个字符而不出现该问题,最好使用for
循环。一个字符一个字符地反向迭代有点困难,但这是可行的:
def nucleo_complement(ifilename, ofilename):
"""Reads a file one character at a time and returns the reverse
complement of each nucleotide."""
complements = {'A': 'T', 'T': 'A', 'C': 'G', 'G': 'C'}
ifile = open(ifilename)
ofile = open(ofilename, 'w')
for pos in range(ifile.seek(0, 2) + 1, 0, -1):
ifile.seek(pos - 1)
char = ifile.read(1)
ofile.write(complements.get(char.upper(), char))
ifile.close()
ofile.close()
seek
返回新的文件位置,seek(0,2)
转到文件中的最后一个字符。每当调用read(1)
时,文件中的位置将前进一个字符,因此我必须让pos
最初等于最后一个字符加上一个字符的位置,并在第二个字符而不是第一个字符处结束循环。对于每个迭代,我返回一个带有ifile.seek(pos-1')
的字符,然后读取下一个(原始)字符。作为一个初学者,这个例子可能有点多,所以如果你有任何问题,请随时提问。实际上,您需要考虑的是for
循环中的前两条语句,以及我同时打开了两个文件的事实。这是工作代码,多亏了Issac。它解决了我遇到的两个问题
def func_rev_seq(in_path,out_path):
"""Read file one character at a time and retrun the reverse complement of each nucleotide to a new file"""
# Write a blank file (out_path)
fb = open(out_path,"w")
fb.write("")
fb.close()
# Dictionary where the key is the nucleotide and the value is its reverse complement
base = {"A":"T", "C":"G", "G":"C", "T":"A", "a":"t", "c":"g", "g":"c", "t":"a", "k":"m", "m":"k", "y":"r", "r":"y", "b":"v", "v":"b", "d":"h", "h":"d", "K":"M", "M":"K", "Y":"R", "R":"Y", "B":"V", "V":"B", "D":"H", "H":"D", "U":"A", "u":"a"}
fi= open(in_path)
fo = open(out_path, 'w')
for pos in range(fi.seek(0, 2) - 1, 0, -1):
fi.seek(pos - 1)
b = fi.read(1)
if b in base:
fo.write(base.get(b, b))
else:
fo.write(b)
fi.close()
fo.close()
顺便说一下,我的代码是正确缩进的,但在这里没有正确呈现,也许我也做错了!空白线下面的字符是什么?它是原始序列中最后一个核苷酸的反向互补。例如,如果原始序列是“AACCTCAGC”,那么它将是一个“G”。谢谢你,这是一个非常有用的解释。我将试一试,让你知道我的进展如何。谢谢。谢谢伊萨克,我仔细考虑了你的建议,只做了一点小小的修改。尽管如此,它确实解决了截断问题,但在输出文件的顶部还有一行。我试着将“范围内的位置(ifile.seek(0,2)+1,0,-1):”改为“范围内的位置(ifile.seek(0,2),0,-1:”,但这似乎没有任何作用,所以我进一步将其改为“范围内的位置(ifile.seek(0,2)-1,0,-1:”。维奥拉!成功了,很高兴能帮上忙。这是有道理的;最后一个字符必须是换行符。没有考虑。