擅长:python、mysql、java
<p>要将一行写入文件,您可以执行以下操作:</p>
<pre><code>with open("file.txt", "a") as f:
print("new line", file=f)
</code></pre>
<p>如果你想让它分开,你也可以添加<code>sep="\t"</code>,这就是为什么python 3制作了<a href="https://docs.python.org/3.8/library/functions.html#print" rel="nofollow noreferrer">^{<cd2>} a function</a>,所以你可以使用<code>sep</code>、<code>end</code>、<code>file</code>和<code>flush</code>关键字参数</p>
<p>打开文件进行追加意味着文件指针从文件末尾开始,这意味着写入文件不会覆盖任何数据(被追加到文件末尾),而对其进行迭代(或从中读取)不会产生任何类似于已到达文件末尾的效果</p>
<p>因此,您不需要迭代文件的行,而只需将单行写入其中:</p>
<pre><code> with open("Mimp_hits.bed", "a") as file_object:
print(sequence.description, h.start(), h_rc.end(), file=file_object)
</code></pre>
<P>你也可以考虑打开一个文件在循环的开始附近,因为打开一次,写多次比打开多次效率更高,而且Fube会自动关闭文件,所以不需要显式地执行。p>