import pandas as pd
# Read data from a CSV file
data = pd.read_csv('data.csv')
# Perform basic analysis
mean = data['column_name'].mean()
print(f"Mean: {mean}")
BeautifulSoup[2]是一个用于网络抓取的 Python 库。它能让你轻松地从网站中提取数据。下面是一个简单的网络抓取脚本:
import requests
from bs4 import BeautifulSoup
url = 'https://example.com'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# Extract data from the webpage
data = soup.find('div', class_='content')
print(data.text)
当你需要根据特定条件重命名文件夹中的多个文件时,这个脚本非常方便。例如,你可以在文件名中添加前缀、后缀或替换文本。
import os
folder_path = '/path/to/folder'
for filename in os.listdir(folder_path):
if filename.startswith('prefix_'):
new_filename = filename.replace('prefix_', 'new_prefix_')
os.rename(os.path.join(folder_path, filename), os.path.join(folder_path, new_filename))
Pillow[3]是一个简化图像处理的 Python 图像库。该脚本可将一批图像的大小调整为指定的分辨率或长宽比:
from PIL import Image
import os
input_folder = '/path/to/images'
output_folder = '/path/to/resized_images'
desired_size = (100, 100)
for filename in os.listdir(input_folder):
with Image.open(os.path.join(input_folder, filename)) as img:
img.thumbnail(desired_size)
img.save(os.path.join(output_folder, filename))
ReportLab[4]是一个用 Python 创建 PDF 文档的库。你可以从文本或 HTML 内容生成 PDF 文件。下面是一个基本示例:
from reportlab.pdfgen import canvas
pdf_file = 'output.pdf'
text = 'Hello, this is a sample PDF.'
c = canvas.Canvas(pdf_file)
c.drawString(100, 750, text)
c.save()
需要自动发送电子邮件?Python 的 smtplib
库可以提供帮助。该脚本可通过编程发送电子邮件:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
smtp_server = 'smtp.example.com'
sender_email = 'your_email@example.com'
receiver_email = 'recipient@example.com'
password = 'your_password'
message = MIMEMultipart()
message['From'] = sender_email
message['To'] = receiver_email
message['Subject'] = 'Sample Email Subject'
body = 'This is a sample email message.'
message.attach(MIMEText(body, 'plain'))
with smtplib.SMTP(smtp_server, 587) as server:
server.starttls()
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message.as_string())
自动备份文件和目录,确保数据安全:
import shutil
source_folder = '/path/to/source_folder'
backup_folder = '/path/to/backup_folder'
shutil.copytree(source_folder, backup_folder)
生成强大的随机密码,提高安全性:
import random
import string
def generate_password(length=12):
characters = string.ascii_letters + string.digits + string.punctuation
return ''.join(random.choice(characters) for _ in range(length))
password = generate_password()
print(password)
创建一个用于测试和开发的基本 HTTP 服务器:
import http.server
import socketserver
port = 8000
with socketserver.TCPServer(('', port), http.server.SimpleHTTPRequestHandler) as httpd:
print(f"Serving at port {port}")
httpd.serve_forever()
SQLite 是一个 C 库,它提供了一个基于磁盘的轻量级数据库,不需要单独的服务器进程,并允许使用 SQL 查询语言的非标准变体访问数据库。一些应用程序可以使用 SQLite 进行内部数据存储。也可以使用 SQLite 制作应用程序原型,然后将代码移植到 PostgreSQL 或 Oracle 等大型数据库。
下面,我将为你提供用 Python 备份和还原 SQLite 数据库的示例代码,这是一种轻量级的常用数据库系统:
import sqlite3
import shutil
# Database file paths
source_db_file = 'source.db'
backup_db_file = 'backup.db'
# Function to create a backup of the SQLite database
def backup_database():
try:
shutil.copy2(source_db_file, backup_db_file)
print("Backup successful.")
except Exception as e:
print(f"Backup failed: {str(e)}")
# Function to restore the SQLite database from a backup
def restore_database():
try:
shutil.copy2(backup_db_file, source_db_file)
print("Restore successful.")
except Exception as e:
print(f"Restore failed: {str(e)}")
# Usage
while True:
print("Options:")
print("1. Backup Database")
print("2. Restore Database")
print("3. Quit")
choice = input("Enter your choice (1/2/3): ")
if choice == '1':
backup_database()
elif choice == '2':
restore_database()
elif choice == '3':
break
else:
print("Invalid choice. Please enter 1, 2, or 3.")
在此代码中
backup_database()
函数复制 SQLite 数据库源文件并将其命名为备份文件。你可以运行此函数来创建数据库备份。restore_database()
函数将备份文件复制回源文件,有效地将数据库恢复到创建备份时的状态。用户可选择备份数据库、恢复数据库或退出程序。 你可以调整 source_db_file
和backup_db_file
变量,指定 SQLite 源文件和备份数据库文件的路径。
Pandas: https://pandas.pydata.org/
[2]BeautifulSoup: https://www.crummy.com/software/BeautifulSoup/
[3]Pillow: https://python-pillow.org/
[4]ReportLab: https://www.reportlab.com/
编辑 /范瑞强
审核 / 范瑞强
复核 / 范瑞强
点击下方
关注我们