Python 文件处理知识点(实际实践)

1. 读取文本文件

with open("data.txt", "r", encoding="utf-8") as f:
    content = f.read()
print(content)

2. 按行读取大文件

with open("data.txt", "r", encoding="utf-8") as f:
    for line in f:
        print(line.strip())

3. 分块读取

chunk_size = 1024 * 1024  # 1MB
with open("big.txt", "r", encoding="utf-8") as f:
    while True:
        chunk = f.read(chunk_size)
        if not chunk:
            break
        # 处理 chunk

4. 写入文本文件

with open("output.txt", "w", encoding="utf-8") as f:
    f.write("Hello\n")

5. JSON 读写

import json

data = {"name": "Alice", "age": 20}
with open("data.json", "w", encoding="utf-8") as f:
    json.dump(data, f, ensure_ascii=False, indent=2)

with open("data.json", "r", encoding="utf-8") as f:
    obj = json.load(f)
print(obj)

6. CSV 读写

import csv

rows = [["name", "age"], ["Alice", 20], ["Bob", 30]]
with open("data.csv", "w", newline="", encoding="utf-8") as f:
    writer = csv.writer(f)
    writer.writerows(rows)

with open("data.csv", "r", encoding="utf-8") as f:
    reader = csv.reader(f)
    for row in reader:
        print(row)

7. Pandas 处理表格文件

import pandas as pd

df = pd.read_csv("data.csv")
print(df.head())

df["age"] = df["age"] + 1

df.to_excel("data.xlsx", index=False)

8. openpyxl 读写 Excel

from openpyxl import Workbook, load_workbook

wb = Workbook()
ws = wb.active
ws.append(["name", "age"])
ws.append(["Alice", 20])
wb.save("demo.xlsx")

wb2 = load_workbook("demo.xlsx")
ws2 = wb2.active
for row in ws2.iter_rows(values_only=True):
    print(row)

9. XML 读取与解析

import xml.etree.ElementTree as ET

tree = ET.parse("data.xml")
root = tree.getroot()

for child in root:
    print(child.tag, child.text)

10. XML 写入

import xml.etree.ElementTree as ET

root = ET.Element("root")
item = ET.SubElement(root, "item")
item.text = "value"

tree = ET.ElementTree(root)
tree.write("out.xml", encoding="utf-8", xml_declaration=True)

11. 文件批处理

from pathlib import Path

folder = Path("data")
for path in folder.glob("*.txt"):
    print(path.name)

12. 文件复制与移动

import shutil

shutil.copy("a.txt", "b.txt")
shutil.move("b.txt", "backup/b.txt")

13. 日志文件处理示例

from pathlib import Path

log_path = Path("app.log")
error_lines = []

with log_path.open("r", encoding="utf-8") as f:
    for line in f:
        if "ERROR" in line:
            error_lines.append(line.strip())

Path("errors.txt").write_text("\n".join(error_lines), encoding="utf-8")

14. 处理二进制文件

with open("image.png", "rb") as f:
    data = f.read()

with open("copy.png", "wb") as f:
    f.write(data)

15. 综合实践流程示例

  • 读取 CSV 或 Excel 数据
  • 清洗缺失值和异常值
  • 处理后输出到 Excel 或 JSON
  • 最终归档压缩
import pandas as pd
import shutil
from pathlib import Path

input_file = "input.csv"
output_file = "output.xlsx"

# 读取
_df = pd.read_csv(input_file)

# 清洗
_df = _df.dropna()
_df = _df.drop_duplicates()

# 输出
_df.to_excel(output_file, index=False)

# 归档
Path("archive").mkdir(exist_ok=True)
shutil.move(output_file, "archive/output.xlsx")
Logo

这里是“一人公司”的成长家园。我们提供从产品曝光、技术变现到法律财税的全栈内容,并连接云服务、办公空间等稀缺资源,助你专注创造,无忧运营。

更多推荐