如何在文件中只写一次增量?
How to write increment only once in a file?
我使用 for 循环读取多个文件,并使用以下代码将输出追加到单独的文件中。我正在寻找的是除了最后一次迭代之外,每次迭代都不要在 count+=1
之后打印行(请参见下文)。
output.write(file_name + ": The total number of reads with two AB sequences is " +
str(count) + '\n')
有没有办法根据我的代码在输出文件的末尾只打印一次最后的 count+=1
?
from Bio import SeqIO
files=['2_.fasta','4_.fasta']
for file_name in files:
count=0
with open(file_name,'r') as f:
for record in SeqIO.parse(f,'fasta'):
#some code
if str(v1) in record.seq and str(v2) in record.seq:
#some code
out = "sample" + file_name[:1] + "_two_AB.txt"
with open(out,'a') as output:
output.write(file_name + '\n')
output.write(">"+str(record.id) + '\n')
count+=1
output.write(file_name + ": The total number of reads with two AB sequences is "
+ str(count) + '\n')
output.close()
希望对您有所帮助(如果我理解您的问题):
在这段代码中,我使用了 list 和 tuple 来保存信息,例如 out,file_name、record.id 和计数。在我在 for 中使用此列表将数据复制到列表中后,到相应的文件中。
我的代码:
from Bio import SeqIO
files=['2_.fasta','4_.fasta']
my_list=[]
for file_name in files:
count=0
with open(file_name,'r') as f:
for record in SeqIO.parse(f,'fasta'):
#some code
if str(v1) in record.seq and str(v2) in record.seq:
#some code
out = "sample" + file_name[:1] + "_two_AB.txt"
with open(out,'a') as output:
output.write(file_name + '\n')
output.write(">"+str(record.id) + '\n')
count+=1
my_list.append(tuple((out,file_name,str(record.id),count)))
#output.write(file_name + ": The total number of reads with two AB sequences is "
# + str(count) + '\n')
output.close()
#Alternative
#for idx,val enumerate(my_list):
# my_out=val[0] # I have out value
# my_file_name=[1] # I have the file_name value
# my_record=val[2] # I have the record.id value
# my_count=val[3] # I have the count value
# with open(my_out, 'a') as output:
# output.write(my_file_name + '\n')
# output.write(">"+ my_record + '\n')
#output.write(my_file_name + ": The total number of reads with two AB sequences is "+ str(my_count))
# output.close()
# -1 in this list is the last file
# my_list[-1][0] Here I have out es."sample" + file_name[:1] + "_two_AB.txt"
# my_list[-1][1] Here I have the file_name
# my_list[-1][2] Here I have the record.id
# my_list[-1][3] Here I have the count
with open(my_list[-1][0], 'a') as output: # my_list[-1][0] is the last file, I have value's "out"
#output.write(my_list[-1][1] + '\n') #my_list[-1][1] i have my_file_name
#output.write(">"+ my_list[-1][2] + '\n') #my_record
output.write(my_list[-1][2] + ": The total number of reads with two AB sequences is "+ str(my_list[-1][3]))
output.close()
我使用 for 循环读取多个文件,并使用以下代码将输出追加到单独的文件中。我正在寻找的是除了最后一次迭代之外,每次迭代都不要在 count+=1
之后打印行(请参见下文)。
output.write(file_name + ": The total number of reads with two AB sequences is " +
str(count) + '\n')
有没有办法根据我的代码在输出文件的末尾只打印一次最后的 count+=1
?
from Bio import SeqIO
files=['2_.fasta','4_.fasta']
for file_name in files:
count=0
with open(file_name,'r') as f:
for record in SeqIO.parse(f,'fasta'):
#some code
if str(v1) in record.seq and str(v2) in record.seq:
#some code
out = "sample" + file_name[:1] + "_two_AB.txt"
with open(out,'a') as output:
output.write(file_name + '\n')
output.write(">"+str(record.id) + '\n')
count+=1
output.write(file_name + ": The total number of reads with two AB sequences is "
+ str(count) + '\n')
output.close()
希望对您有所帮助(如果我理解您的问题):
在这段代码中,我使用了 list 和 tuple 来保存信息,例如 out,file_name、record.id 和计数。在我在 for 中使用此列表将数据复制到列表中后,到相应的文件中。
我的代码:
from Bio import SeqIO
files=['2_.fasta','4_.fasta']
my_list=[]
for file_name in files:
count=0
with open(file_name,'r') as f:
for record in SeqIO.parse(f,'fasta'):
#some code
if str(v1) in record.seq and str(v2) in record.seq:
#some code
out = "sample" + file_name[:1] + "_two_AB.txt"
with open(out,'a') as output:
output.write(file_name + '\n')
output.write(">"+str(record.id) + '\n')
count+=1
my_list.append(tuple((out,file_name,str(record.id),count)))
#output.write(file_name + ": The total number of reads with two AB sequences is "
# + str(count) + '\n')
output.close()
#Alternative
#for idx,val enumerate(my_list):
# my_out=val[0] # I have out value
# my_file_name=[1] # I have the file_name value
# my_record=val[2] # I have the record.id value
# my_count=val[3] # I have the count value
# with open(my_out, 'a') as output:
# output.write(my_file_name + '\n')
# output.write(">"+ my_record + '\n')
#output.write(my_file_name + ": The total number of reads with two AB sequences is "+ str(my_count))
# output.close()
# -1 in this list is the last file
# my_list[-1][0] Here I have out es."sample" + file_name[:1] + "_two_AB.txt"
# my_list[-1][1] Here I have the file_name
# my_list[-1][2] Here I have the record.id
# my_list[-1][3] Here I have the count
with open(my_list[-1][0], 'a') as output: # my_list[-1][0] is the last file, I have value's "out"
#output.write(my_list[-1][1] + '\n') #my_list[-1][1] i have my_file_name
#output.write(">"+ my_list[-1][2] + '\n') #my_record
output.write(my_list[-1][2] + ": The total number of reads with two AB sequences is "+ str(my_list[-1][3]))
output.close()