File size: 1,607 Bytes
ba38a19 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
{
"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
}
|