題目:https://rosalind.info/problems/ini5/
讀入一個充滿文字的檔案
按照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讀檔操作的幾種姿勢:
創建一個example.txt
Hello
World
Python
with open("IN5.txt", "r", encoding="utf-8") as file:
content = file.read() # 一次讀全部
print(content)
# Hello
# World
# Python
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
使用迴圈逐行讀取
with open("example.txt", "r", encoding="utf-8") as file:
for line in file:
print(line.strip()) # 每次讀取一行
# Hello
# World
# Python
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
from pathlib import Path
content = Path("example.txt").read_text(encoding="utf-8") # 一次讀一整個檔案
# .read_bytes() # 則是讀二進制檔案
print(content)
# Hello
# World
# Python
寫法更短,甚至不用寫open()、也不用with,因為都包好了。是更Pythonic的方式
file = open("IN5.txt", "r", encoding="utf-8")
try:
content = file.read()finally:
file.close() # 手動關閉文件流,確保資源釋放
print(content)
# 只有 def, class 區塊會創建新的作用域(scope)
# if, for, while, with, try/catch 區塊不會創建作用域,所以content變數仍存在
忘記關閉的話會導致記憶體洩漏