iT邦幫忙

2025 iThome 鐵人賽

DAY 6
0
AI & Data

Rosalind 生物資訊解題系統系列 第 6

Day06 | Rosalind 生資解題 - IN5. Working with Files +讀檔操作

  • 分享至 

  • xImage
  •  

Day06 | Rosalind 生資解題 - IN5. Working with Files +讀檔操作

題目連結:https://rosalind.info/problems/ini5/

https://ithelp.ithome.com.tw/upload/images/20250917/20125192uS6qpPcdNs.png

讀入一個充滿文字的檔案
按照1-based 索引取出偶數行(第2、4、6... 行)

程式碼:

with open("IN5.txt", "r", encoding="utf-8") as file:
    lines = file.readlines()
    for i, line in enumerate(lines, start=1):  # 索引從1(第一行)開始,以符合直覺
        if i % 2 == 0:
            print(line.strip())

僅印出偶數行的文字即可


Python讀檔操作

這邊首先複習一下Python讀檔操作的幾種姿勢
創建一個example.txt

Hello
World
Python

1:一次讀全部,讀進來變成大字串。適合小型文件

with open("example.txt", "r", encoding="utf-8") as file:
    content = file.read()  # 一次讀全部

print(content)

# Hello
# World
# Python​

2:一次讀全部,讀近來變成列表。適合小型文件

2-1:去除換行符號

with open("example.txt", "r", encoding="utf-8") as file:
    lines = file.readlines()  # 讀取所有行,回傳 list

for line in lines:
    print(line.strip())   # strip() 移除換行符號

# Hello
# World
# Python

2-2:不去除換行符號

with open("example.txt", "r", encoding="utf-8") as file:
    lines = file.readlines()  # 讀取所有行,回傳 list

for line in lines:
    print(line)

# Hello

# World

# Python

為何會多一個換行?因為print()函式在行尾會自動加上換行字元
所以2-1是把文件原本的\n去除掉,就只會印出一行換行

2-3:印出原始字符

with open("example.txt", "r", encoding="utf-8") as file:
    lines = file.readlines()  # 讀取所有行,回傳 list

for line in lines:
    print(repr(line))  # 讀取格式字符

# 'Hello\n'
# 'World\n'
# 'Python'

這樣就可以看出,2-1是把文件的\n去除掉了,而非去除print()函式的\n

3:一次只讀一行。可讀大型文件

使用迴圈逐行讀取
with open("example.txt", "r", encoding="utf-8") as file:
    for line in file:
        print(line.strip())  # 每次讀取一行

# Hello
# World
# Python​

4:讀取固定長度字節

with open("example.txt", "r", encoding="utf-8") as file:
    chunk = file.read(1024)  # 每次讀取 1024 字節
    while chunk:
        print(chunk)
        chunk = file.read(1024)

# Hello
# World
# Python​

5:與1.相同,但更簡潔(pathlib.Path)

from pathlib import Path
content = Path("example.txt").read_text(encoding="utf-8")  # 一次讀一整個檔案
# .read_bytes() # 則是讀二進制檔案

print(content)

# Hello
# World
# Python

寫法更短,甚至不用寫open()、也不用with,因為都包好了。是更Pythonic的方式

6:沒有with,則需要手動關閉文件(不推薦)

file = open("example.txt", "r", encoding="utf-8")
try:
    content = file.read()finally:
    file.close()  # 手動關閉文件流,確保資源釋放

print(content)
# 只有 def, class 區塊會創建新的作用域(scope)
# if, for, while, with, try/catch 區塊不會創建作用域,所以content變數仍存在

忘記關閉的話會導致記憶體洩漏


上一篇
Day05 | Rosalind 生資解題 - IN4. Conditions and Loops +奇偶數判斷
下一篇
Day07 | Rosalind 生資解題 - IN6. Dictionaries +defaultdict
系列文
Rosalind 生物資訊解題系統7
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言