Python 提供了多种强大的工具和库,可以轻松实现对各种类型文件的读写操作,满足不同场景的数据处理需求。常见的文件类型包括文本文件(txt)、表格文件(CSV、Excel)、结构化数据文件(JSON、YAML、XML)、二进制数据文件(Parquet)、数据库文件(SQLite),以及其他格式如日志文件(log)、压缩文件(ZIP)和PDF文件等。通过内置的 open 函数和标准库模块如 csv、json、sqlite3 等,以及第三方库如 pandas、yaml、fpdf 等,Python 能够快速实现对这些文件的读写操作。这种灵活性和多样性使得 Python 成为处理数据、开发应用和实现自动化工作的首选编程语言之一。
Python 读写 txt 文件
# 写入 TXT 文件with open('example.txt', 'w', encoding='utf-8') as file: file.write("你好,Python 文件读写示例!\n第二行内容。") # 读取 TXT 文件with open('example.txt', 'r', encoding='utf-8') as file: content = file.read() print(content)
Python 读写 CSV 文件
import csv# 写入 CSV 文件with open('example.csv', 'w', newline='', encoding='utf-8') as file: writer = csv.writer(file) writer.writerow(["姓名", "年龄", "城市"]) writer.writerow(["张三", 25, "北京"]) writer.writerow(["李四", 30, "上海"]) # 读取 CSV 文件with open('example.csv', 'r', encoding='utf-8') as file: reader = csv.reader(file) for row in reader: print(row)
Python 读写 Excel 文件
import pandas as pd# 写入 Excel 文件data = {'姓名': ['张三', '李四'], '年龄': [25, 30], '城市': ['北京', '上海']}df = pd.DataFrame(data)df.to_excel('example.xlsx', index=False) # 读取 Excel 文件df_read = pd.read_excel('example.xlsx')print(df_read)
Python 读写 JSON 文件
import json# 写入 JSON 文件data = {'name': '张三', 'age': 25, 'city': '北京'}with open('example.json', 'w', encoding='utf-8') as file: json.dump(data, file, ensure_ascii=False, indent=4) # 读取 JSON 文件with open('example.json', 'r', encoding='utf-8') as file: data_read = json.load(file) print(data_read)
Python 读写 SQLite 数据库
import sqlite3# 创建 SQLite 数据库并写入数据conn = sqlite3.connect('example.db')cursor = conn.cursor()cursor.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)")cursor.execute("INSERT INTO users (name, age) VALUES (?, ?)", ('张三', 25))conn.commit() # 读取 SQLite 数据库数据cursor.execute("SELECT * FROM users")rows = cursor.fetchall()for row in rows: print(row)conn.close()
Python 读写 XML 文件
import xml.etree.ElementTree as ET# 写入 XML 文件root = ET.Element("root")user = ET.SubElement(root, "user")ET.SubElement(user, "name").text = "张三"ET.SubElement(user, "age").text = "25"tree = ET.ElementTree(root)tree.write("example.xml", encoding='utf-8', xml_declaration=True) # 读取 XML 文件tree = ET.parse('example.xml')root = tree.getroot()for elem in root.iter(): print(elem.tag, elem.text)
Python 读写 Parquet 文件
import pandas as pd# 写入 Parquet 文件data = {'姓名': ['张三', '李四'], '年龄': [25, 30], '城市': ['北京', '上海']}df = pd.DataFrame(data)df.to_parquet('example.parquet', index=False) # 读取 Parquet 文件df_read = pd.read_parquet('example.parquet')print(df_read)
Python 读写 YAML 文件
import yaml# 写入 YAML 文件data = {'姓名': '张三', '年龄': 25, '城市': '北京'}with open('example.yaml', 'w', encoding='utf-8') as file: yaml.dump(data, file, allow_unicode=True) # 读取 YAML 文件with open('example.yaml', 'r', encoding='utf-8') as file: data_read = yaml.safe_load(file) print(data_read)
Python 读写 INI 文件
import configparser# 写入 INI 文件config = configparser.ConfigParser()config['DEFAULT'] = {'Server': 'localhost', 'Port': '8080'}with open('example.ini', 'w', encoding='utf-8') as configfile: config.write(configfile) # 读取 INI 文件config.read('example.ini', encoding='utf-8')print(dict(config['DEFAULT'])) Python 读写 PDF 文件from fpdf import FPDFfrom PyPDF2 import PdfReader# 写入 PDF 文件pdf = FPDF()pdf.add_page()pdf.set_font('Arial', size=12)pdf.cell(200, 10, txt="Python PDF 文件", ln=True, align='C')pdf.output("example.pdf") # 读取 PDF 文件reader = PdfReader("example.pdf")for page in reader.pages: print(page.extract_text())
Python 读写 ZIP 文件
import zipfile# 写入 ZIP 文件with zipfile.ZipFile('example.zip', 'w') as zipf: zipf.write('example.txt') # 解压 ZIP 文件with zipfile.ZipFile('example.zip', 'r') as zipf: zipf.extractall('output')
Python 读写 Log 文件
import logging# 写入日志logging.basicConfig(filename='example.log', level=logging.INFO, format='%(asctime)s - %(message)s')logging.info("这是一个日志信息")logging.warning("这是一个警告信息")logging.error("这是一个错误信息") # 读取日志with open('example.log', 'r', encoding='utf-8') as file: logs = file.read() print(logs)