2021-06-19 20:51:08
Python 文件读写操作可通过内置的 open() 函数结合不同模式实现,同时可借助 json、csv、pandas、pickle 等模块处理特定格式文件。 以下是详细方法及示例:
一、基础文件读写操作文件读取
读取全部内容:使用 read() 方法一次性读取文件所有内容。
with open('example.txt', 'r') as file: content = file.read() print(content)逐行读取:
通过 readlines() 将内容按行存储为列表,再遍历列表。
直接迭代文件对象(更高效,无需额外存储列表)。
with open('example.txt', 'r') as file: # 方法1:readlines() lines = file.readlines() for line in lines: print(line.strip()) # 去除行末换行符 # 方法2:直接迭代 file.seek(0) # 重置指针到开头 for line in file: print(line.strip())文件写入
覆盖写入:使用模式 'w',若文件存在则清空内容,不存在则创建。
with open('output.txt', 'w') as file: file.write("Hello, World!n") # 需手动添加换行符 file.write("This is a new line.")文件追加
使用模式 'a',在文件末尾添加内容,不覆盖原有数据。
with open('output.txt', 'a') as file: file.write("nThis is an appended line.")二进制读取
使用模式 'rb' 读取二进制文件(如图片、音频等)。
with open('example.bin', 'rb') as file: binary_data = file.read() print(binary_data) # 输出二进制数据(如 b'x48x65...')二进制写入
使用模式 'wb' 写入二进制数据,需通过 b'' 前缀定义二进制内容。
with open('output.bin', 'wb') as file: binary_data = b'x48x65x6cx6cx6fx20x57x6fx72x6cx64' # "Hello World"的二进制 file.write(binary_data)JSON 文件
写入:使用 json.dump() 将 Python 对象序列化为 JSON 格式。
读取:使用 json.load() 将 JSON 数据反序列化为 Python 对象。
import json# 写入data = {'name': 'John', 'age': 30, 'city': 'New York'}with open('data.json', 'w') as file: json.dump(data, file)# 读取with open('data.json', 'r') as file: loaded_data = json.load(file) print(loaded_data) # 输出: {'name': 'John', 'age': 30, 'city': 'New York'}CSV 文件
写入:使用 csv.writer 逐行写入数据,需指定 newline='' 避免空行。
读取:使用 csv.reader 逐行读取数据为列表。
import csv# 写入data = [['Name', 'Age', 'City'], ['John', 30, 'New York'], ['Jane', 25, 'Chicago']]with open('data.csv', 'w', newline='') as file: writer = csv.writer(file) writer.writerows(data)# 读取with open('data.csv', 'r') as file: reader = csv.reader(file) for row in reader: print(row) # 输出每行数据列表,如 ['Name', 'Age', 'City']Pandas 处理结构化数据
写入:通过 DataFrame.to_csv() 将数据框保存为 CSV 文件。
读取:通过 pd.read_csv() 读取 CSV 文件为数据框。
import pandas as pd# 写入data = {'Name': ['John', 'Jane'], 'Age': [30, 25], 'City': ['New York', 'Chicago']}df = pd.DataFrame(data)df.to_csv('data_pandas.csv', index=False) # 不保存索引# 读取df_read = pd.read_csv('data_pandas.csv')print(df_read)通过上述方法,可灵活应对不同场景下的文件操作需求。