编程开发案例
批量文件重命名工具
利用 Python 内置的 OS 模块,快速遍历指定目录下的所有文件,并根据预设规则(如添加日期前缀或修改扩展名)进行批量重命名,极大地提高了文件整理效率。
import os
folder = './documents'
for filename in os.listdir(folder):
if filename.endswith('.txt'):
new_name = 'backup_' + filename
os.rename(os.path.join(folder, filename),
os.path.join(folder, new_name))
查看详情
简易网页内容获取
使用 Requests 库获取网页 HTML 内容,并配合 BeautifulSoup 解析库提取特定的新闻标题和链接。该脚本展示了如何自动化收集网络信息。
import requests
from bs4 import BeautifulSoup
res = requests.get('https://news.example.com')
soup = BeautifulSoup(res.text, 'html.parser')
for news in soup.find_all('h2', class_='title'):
print(news.get_text().strip())
查看详情
CSV 数据自动化分析
借助 Pandas 强大的一维和二维数据处理能力,快速读取 CSV 格式的销售报表,计算总销售额并筛选出业绩达标的数据行,是数据分析的入门经典案例。
import pandas as pd
df = pd.read_csv('sales_data.csv')
total_sales = df['amount'].sum()
# 筛选销售额大于 1000 的记录
high_performers = df[df['amount'] > 1000]
print(high_performers.head())
查看详情