{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "1e6f3bc7-5852-445f-b3d8-c183457a2052", "metadata": {}, "outputs": [], "source": [ "# 打开三个输入文件和输出文件\n", "with open('dna_4g.txt', 'r', encoding='utf-8') as f1, \\\n", " open('eng_4g.txt', 'r', encoding='utf-8') as f2, \\\n", " open('protein_4g.txt', 'r', encoding='utf-8') as f3, \\\n", " open('dna_eng_protein_12g.txt', 'w', encoding='utf-8') as output:\n", "\n", " # 使用迭代器读取行\n", " iterators = [iter(f1), iter(f2), iter(f3)]\n", "\n", " while True:\n", " exhausted_count = 0\n", " for it in iterators:\n", " try:\n", " # 从当前文件取一行\n", " line = next(it).strip()\n", " output.write(line + '\\n')\n", " except StopIteration:\n", " exhausted_count += 1\n", "\n", " if exhausted_count == len(iterators):\n", " break\n", "\n", "print(\"混合完成!新文件: dna_eng_protein_12g.txt\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.3" } }, "nbformat": 4, "nbformat_minor": 5 }