屏东县网站建设_网站建设公司_Sketch_seo优化
2026/1/14 22:58:57 网站建设 项目流程

Python 标准库实战

datetime 模块处理日期

from datetime import datetime, timedelta # 获取当前时间 now = datetime.now() print(f"当前时间: {now}") # 时间加减操作 next_week = now + timedelta(days=7) print(f"一周后时间: {next_week}") # 时间格式化 formatted = now.strftime("%Y-%m-%d %H:%M:%S") print(f"格式化时间: {formatted}")

https://www.zhihu.com/zvideo/1994903430897112174/
https://www.zhihu.com/zvideo/1994903428737045479/
https://www.zhihu.com/zvideo/1994903429080949969/
https://www.zhihu.com/zvideo/1994903429131289981/
https://www.zhihu.com/zvideo/1994903428959331036/
https://www.zhihu.com/zvideo/1994903428321805979/
https://www.zhihu.com/zvideo/1994903426161713909/
https://www.zhihu.com/zvideo/1994903426757326323/
https://www.zhihu.com/zvideo/1994903426111390567/
https://www.zhihu.com/zvideo/1994903426061054719/
https://www.zhihu.com/zvideo/1994903425146720275/
https://www.zhihu.com/zvideo/1994903425100580088/
https://www.zhihu.com/zvideo/1994903424815346534/
https://www.zhihu.com/zvideo/1994903423666128232/
https://www.zhihu.com/zvideo/1994903422940512958/

os 模块文件操作

import os # 检查文件是否存在 file_path = "example.txt" if os.path.exists(file_path): print(f"{file_path} 存在") # 获取当前工作目录 current_dir = os.getcwd() print(f"当前目录: {current_dir}") # 创建新目录 new_dir = "temp_folder" os.makedirs(new_dir, exist_ok=True)

第三方库高效应用

requests 网络请求

import requests # GET 请求示例 response = requests.get("https://api.github.com") print(f"状态码: {response.status_code}") print(f"响应内容: {response.json()}")

pandas 数据处理

import pandas as pd # 创建DataFrame data = {"Name": ["Alice", "Bob"], "Age": [25, 30]} df = pd.DataFrame(data) # 数据筛选 filtered = df[df["Age"] > 25] print(filtered)

Excel 自动化操作

openpyxl 基础操作

from openpyxl import Workbook # 创建工作簿 wb = Workbook() ws = wb.active # 写入数据 ws["A1"] = "Hello" ws["B1"] = "World" # 保存文件 wb.save("example.xlsx")

pandas 读写 Excel

# 读取Excel excel_data = pd.read_excel("data.xlsx") # 写入Excel excel_data.to_excel("output.xlsx", index=False)

实用代码片段

进度条显示

from tqdm import tqdm import time for i in tqdm(range(100)): time.sleep(0.1) # 模拟任务

日志记录

import logging logging.basicConfig(filename="app.log", level=logging.INFO) logging.info("程序启动") logging.warning("需要注意的情况")

环境变量管理

from dotenv import load_dotenv import os load_dotenv() api_key = os.getenv("API_KEY")

这些代码示例覆盖了Python开发中常见场景,从基础日期处理到复杂数据操作,可直接应用于实际项目中。每个代码块都经过验证,确保功能完整性和实用性。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询