Python 文件读写的八大实用方法

开发
本文我们就来探讨 Python 文件读写的八大实用方法,并通过实际代码示例逐步深入。

Python作为一门强大的编程语言,不仅在数据处理、机器学习等领域大放异彩,其文件操作功能也非常强大且灵活。无论是读取配置文件、处理日志文件,还是编写脚本进行文件自动化管理,Python都提供了丰富的方法。今天,我们就来探讨Python文件读写的8大实用方法,并通过实际代码示例逐步深入。

1. 使用open函数读取文件

在Python中,读取文件最基本的方式是使用内置的open函数。这个函数返回一个文件对象,你可以用它来读取文件内容。

# 打开文件并读取内容  
with open('example.txt', 'r', encoding='utf-8') as file:  
    content = file.read()  
    print(content)  

解释:

  • open('example.txt', 'r', encoding='utf-8'):打开当前目录下的example.txt文件,模式'r'表示读取模式,encoding='utf-8'指定文件编码。
  • with语句确保文件在读取完毕后自动关闭。
  • file.read():读取文件全部内容。

2. 按行读取文件

对于大文件,一次性读取全部内容可能会消耗大量内存。按行读取是一个更好的选择。

with open('example.txt', 'r', encoding='utf-8') as file:  
    for line in file:  
        print(line.strip())  # strip()去除行尾的换行符  

解释:

  • for line in file:逐行读取文件内容。
  • line.strip():去除每行末尾的换行符。

3. 写入文件

写入文件同样使用open函数,但模式要改为'w'(写入)或'a'(追加)。

# 写入文件  
with open('output.txt', 'w', encoding='utf-8') as file:  
    file.write('Hello, Python!\n')  
    file.write('Welcome to file operations.')  

# 追加内容到文件  
with open('output.txt', 'a', encoding='utf-8') as file:  
    file.write('\nGoodbye, Python!')  

解释:

  • 'w'模式会覆盖文件原有内容,'a'模式会在文件末尾追加内容。
  • file.write():将字符串写入文件。

4. 使用readlines读取所有行

readlines方法会读取文件所有行,并返回一个包含每行内容的列表。

with open('example.txt', 'r', encoding='utf-8') as file:  
    lines = file.readlines()  
    for line in lines:  
        print(line.strip())  

解释:

  • file.readlines():读取所有行,每行作为列表的一个元素。

5. 写入多行

你可以使用writelines方法一次性写入多行内容。

lines_to_write = ['First line.\n', 'Second line.\n', 'Third line.\n']  
with open('output.txt', 'w', encoding='utf-8') as file:  
    file.writelines(lines_to_write)  

解释:

  • writelines接受一个字符串列表,每个字符串代表文件的一行。

6. 使用pickle模块序列化对象

pickle模块可以将Python对象序列化为字节流,然后写入文件,或者从文件中反序列化对象。

import pickle  

# 序列化对象并写入文件  
data = {'name': 'Alice', 'age': 30, 'city': 'New York'}  
with open('data.pkl', 'wb') as file:  
    pickle.dump(data, file)  

# 从文件中反序列化对象  
with open('data.pkl', 'rb') as file:  
    loaded_data = pickle.load(file)  
    print(loaded_data)  

解释:

  • 'wb'和'rb'模式分别用于二进制写入和读取。
  • pickle.dump(data, file):将data对象序列化并写入文件。
  • pickle.load(file):从文件中反序列化对象。

7. 使用csv模块处理CSV文件

CSV(逗号分隔值)文件是常见的数据存储格式。Python的csv模块提供了方便的接口来读写CSV文件。

import csv  

# 写入CSV文件  
with open('data.csv', 'w', newline='', encoding='utf-8') as file:  
    writer = csv.writer(file)  
    writer.writerow(['Name', 'Age', 'City'])  
    writer.writerows([['Alice', 30, 'New York'], ['Bob', 25, 'Los Angeles']])  

# 读取CSV文件  
with open('data.csv', 'r', encoding='utf-8') as file:  
    reader = csv.reader(file)  
    for row in reader:  
        print(row)  

解释:

  • csv.writer(file):创建一个写入器对象。
  • writer.writerow(row)和writer.writerows(rows):写入一行或多行数据。
  • csv.reader(file):创建一个读取器对象。
  • for row in reader:逐行读取CSV文件内容。

8. 使用json模块处理JSON文件

JSON(JavaScript Object Notation)是一种轻量级的数据交换格式。Python的json模块可以方便地进行JSON数据的序列化和反序列化。

import json  

# 序列化对象并写入JSON文件  
data = {'name': 'Alice', 'age': 30, 'city': 'New York'}  
with open('data.json', 'w', encoding='utf-8') as file:  
    json.dump(data, file, ensure_ascii=False, indent=4)  

# 从JSON文件中反序列化对象  
with open('data.json', 'r', encoding='utf-8') as file:  
    loaded_data = json.load(file)  
    print(loaded_data)  

解释:

  • json.dump(data, file, ensure_ascii=False, indent=4):将data对象序列化为JSON格式并写入文件,ensure_ascii=False确保非ASCII字符正确显示,indent=4使输出格式化。
  • json.load(file):从文件中反序列化JSON数据。

实战案例:日志文件分析

假设你有一个日志文件log.txt,内容如下:

2023-10-01 10:00:00 INFO User logged in  
2023-10-01 10:05:00 ERROR Invalid credentials  
2023-10-01 10:10:00 INFO User logged out  

你的任务是统计日志文件中每个日志级别的出现次数。

from collections import defaultdict  

log_levels = defaultdict(int)  

with open('log.txt', 'r', encoding='utf-8') as file:  
    for line in file:  
        timestamp, level, message = line.split(maxsplit=2)  
        log_levels[level] += 1  

print("Log levels count:")  
for level, count in log_levels.items():  
    print(f"{level}: {count}")  

输出:

Log levels count:  
INFO: 2  
ERROR: 1  

分析:- 我们使用defaultdict(int)来统计每个日志级别的出现次数。- 通过line.split(maxsplit=2)将每行日志分割为时间戳、日志级别和消息三部分。- 遍历文件,更新日志级别的计数。- 最后打印出每个日志级别的出现次数。

总结

本篇文章详细介绍了Python文件读写的8大实用方法,包括基本的文件读写操作、逐行读取、写入多行、使用pickle、csv和json模块处理特定格式的文件。通过实际代码示例,我们逐步深入,从简单到复杂,展示了每个概念是如何应用的。最后,通过一个实战案例——日志文件分析,进一步巩固了所学知识。

责任编辑:赵宁宁 来源: 手把手PythonAI编程
相关推荐

2009-09-15 15:51:52

2009-06-22 14:07:46

JSF优势

2010-10-27 14:17:19

UI设计布局

2024-07-16 14:52:31

扩展系统负载均衡器

2015-10-08 09:08:50

Python实现

2011-04-29 09:15:16

Servlet

2024-04-24 09:52:19

云技能云迁移云计算

2012-05-10 16:45:54

linux系统

2023-12-27 11:45:09

2025-01-02 12:51:06

2022-04-25 13:11:14

Python编程技巧

2024-07-23 20:33:32

2022-01-05 09:26:56

IT灾难IT故障

2011-08-17 13:55:25

VoIPPBX

2020-06-28 14:01:50

漏洞管理漏洞攻击

2011-07-11 14:01:12

JAVA

2012-05-11 11:53:36

虚拟化

2012-05-05 09:28:50

三星

2011-12-19 09:35:53

2011-04-14 18:03:49

点赞
收藏

51CTO技术栈公众号