终极Python备忘单:日常任务的实用Python

2024-06-17 01:20

本文主要是介绍终极Python备忘单:日常任务的实用Python,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本文是一篇节选翻译,原文: Ultimate Python Cheat Sheet: Practical Python For Everyday Tasks

选取了原文中最常见的python操作,对于数据库交互,科学计算等相对领域化的内容没有选取,有需要的可以直接读原文.

如需要PDF方便查阅可留言或私信.

文章目录

    • 概述
    • 文件操作
      • 读文件
      • 写文件
      • 追加写入文件
      • 读取到List
      • 遍历读取文件
      • 检查文件是否存在
      • 向文件中写入List
      • with语句块操作多个文件
      • 删除文件
      • 读写二进制文件
    • HTTP交互
      • 简单GET请求
      • 带参数的GET请求
      • HTTP错误处理
      • 设置请求超时时间
      • 设置HTTP Header
      • JSON格式数据的POST请求
      • 处理Response编码
      • 使用Session
      • 处理Redirects
      • 流式处理Response
    • 列表操作
      • 创建List
      • 追加到List
      • 列表插入操作
      • 从列表中移除元素
      • 从列表中弹出元素
      • 查找元素的索引
      • 列表切片
      • 列表推导
      • 列表排序
      • 列表翻转
    • 字典操作
      • 创建字典
      • 新增或更新字典项
      • 移除字典项
      • 检查Key是否存在
      • 遍历字典的Key
      • 遍历字典的Value
      • 遍历字典项
      • 字典推导
      • 字典合并
      • 带默认值获取字典值
    • 操作系统交互
      • 路径创建和解析
      • 列举目录内容
      • 创建目录
      • 移除文件或者目录
      • 执行shell命令
      • 环境变量交互
      • 切换当前工作目录
      • 判断路径是否存在以及路径的文件类型
      • 使用临时文件
      • 获取系统信息
    • 命令行交互 -- STDIN,SRDOUT,STDERR
      • 读取用户输入
      • 打印到STDOUT
      • 格式化输出
      • 从STDIN逐行读取
      • 输出到STDERR
      • 重定向STDOUT
      • 重定向STDERR
      • 控制台读取密码
      • 命令行参数
      • Argparse实现复杂命令行交互
    • 使用Decorator
      • 简单Decorator
      • 带参数的Decorator
      • 使用`functools.wraps`
      • Class Decorator
      • 带参数的Decorator
      • Method Decorator
      • 堆叠Decorator
      • Decorator使用可选参数
      • 类方法Decorator
      • 静态方法Decorator
    • 字符串操作
      • 字符串拼接
      • 字符串格式化
      • 字符串大小写转换
      • 字符串操作 `strip,rstrip,lstrip`
      • 字符串操作 `startswith,endswith`
      • 字符串操作 `split,join`
      • 字符串操作 `replace`
      • 字符串操作`find,index`
      • 字符串操作`isdigit,isalpha,isalnum`
      • 字符串切片

概述

这个cheat sheet是一份应需求而有的产物。最近,我被要求深入研究一个新的Python项目,但我已经长时间没有使用python了.

我一直欣赏Python的实用语法和形式。然而,在Node/Typescript领域待了一段时间后,我发现自己需要快速复习Python的最新特性、最佳实践和最有影响力的工具。我需要快速恢复状况,而不被细枝末节所困扰,所以我整理了这个列表,以便可以查阅我经常需要使用的任务和功能。基本上,这个备忘单帮助我掌握了解决80%编程需求的Python基本20%。

这个指南是那段过程的总结,提供了我遇到的最实用的Python知识、见解和有用的库的集合。它旨在分享我发现最有价值的学习,以一种立即适用于你的项目和挑战的方式呈现。

我把各个部分分成了逻辑区域,通常一起工作,这样你就可以跳到你感兴趣的区域,并找到与特定任务或主题最相关的条目。这将包括文件操作、API交互、电子表格操作、数学计算以及与列表和字典等数据结构的工作。此外,我还将介绍一些有用的库,以增强你的Python工具包,在Python通常使用的领域中很常见。

文件操作

读文件

从文件中读取所有内容

with open('example.txt', 'r') as file:content = file.read()print(content)

写文件

向文件中写入文本,覆盖已经存在的内容

with open('example.txt', 'w') as file:file.write('Hello, Python!')

追加写入文件

向文件中追加写入文本

with open('example.txt', 'a') as file:file.write('\nAppend this line.')

读取到List

读取文件内容到一个List中

with open('example.txt', 'r') as file:lines = file.readlines()print(lines)

遍历读取文件

iterate方式读取

with open('example.txt', 'r') as file:for line in file:print(line.strip())

检查文件是否存在

操作文件之前,检查文件是否存在

import os
if os.path.exists('example.txt'):print('File exists.')
else:print('File does not exist.')

向文件中写入List

把List中的每一行写入文件

lines = ['First line', 'Second line', 'Third line']
with open('example.txt', 'w') as file:for line in lines:file.write(f'{line}\n')

with语句块操作多个文件

用with语句块同时操作多个文件

with open('source.txt', 'r') as source, open('destination.txt', 'w') as destination:content = source.read()destination.write(content)

删除文件

安全的删除文件,删除前先判断文件是否存在

import os
if os.path.exists('example.txt'):os.remove('example.txt')print('File deleted.')
else:print('File does not exist.')

读写二进制文件

用二进制方式读写文件,对图片,视频的场景比较适用

# Reading a binary file
with open('image.jpg', 'rb') as file:content = file.read()
# Writing to a binary file
with open('copy.jpg', 'wb') as file:file.write(content)

HTTP交互

译者注:这里要依赖requests库, 用命令安装: pip install requests

简单GET请求

GET请求获取数据

import requests
response = requests.get('https://api.example.com/data')
data = response.json()  # Assuming the response is JSON
print(data)

带参数的GET请求

带有query参数的GET请求

import requests
params = {'key1': 'value1', 'key2': 'value2'}
response = requests.get('https://api.example.com/search', params=params)
data = response.json()
print(data)

HTTP错误处理

对可能出现的异常进行处理

import requests
response = requests.get('https://api.example.com/data')
try:response.raise_for_status()  # Raises an HTTPError if the status is 4xx, 5xxdata = response.json()print(data)
except requests.exceptions.HTTPError as err:print(f'HTTP Error: {err}')

设置请求超时时间

设置请求超时时间,放置无限挂起

import requests
try:response = requests.get('https://api.example.com/data', timeout=5)  # Timeout in secondsdata = response.json()print(data)
except requests.exceptions.Timeout:print('The request timed out')

设置HTTP Header

在请求中设置HTTP Header, 例如在需要使用Authoriztion的场景

import requests
headers = {'Authorization': 'Bearer YOUR_ACCESS_TOKEN'}
response = requests.get('https://api.example.com/protected', headers=headers)
data = response.json()
print(data)

JSON格式数据的POST请求

json格式的POST请求

import requests
payload = {'key1': 'value1', 'key2': 'value2'}
headers = {'Content-Type': 'application/json'}
response = requests.post('https://api.example.com/submit', json=payload, headers=headers)
print(response.json())

处理Response编码

import requests
response = requests.get('https://api.example.com/data')
response.encoding = 'utf-8'  # Set encoding to match the expected response format
data = response.text
print(data)

使用Session

import requests
with requests.Session() as session:session.headers.update({'Authorization': 'Bearer YOUR_ACCESS_TOKEN'})response = session.get('https://api.example.com/data')print(response.json())

处理Redirects

在请求中处理或者禁用redirect

import requests
response = requests.get('https://api.example.com/data', allow_redirects=False)
print(response.status_code)

流式处理Response

对于size很大的response, 可以用流式分块处理

import requests
response = requests.get('https://api.example.com/large-data', stream=True)
for chunk in response.iter_content(chunk_size=1024):process(chunk)  # Replace 'process' with your actual processing function

列表操作

创建List

# A list of mystical elements
elements = ['Earth', 'Air', 'Fire', 'Water']

追加到List

elements.append('Aether')

列表插入操作

在指定位置插入数据

# Insert 'Spirit' at index 1
elements.insert(1, 'Spirit')

从列表中移除元素

根据元素值从列表中移除元素

elements.remove('Earth')  # Removes the first occurrence of 'Earth'

从列表中弹出元素

删除并返回给定索引处的元素(默认为最后一个元素)

last_element = elements.pop()  # Removes and returns the last element

查找元素的索引

index_of_air = elements.index('Air')

列表切片

# Get elements from index 1 to 3
sub_elements = elements[1:4]

列表推导

通过对现有列表中的每个元素应用表达式来创建新列表

# Create a new list with lengths of each element
lengths = [len(element) for element in elements]

列表排序

elements.sort()

列表翻转

elements.reverse()

字典操作

创建字典

# A tome of elements and their symbols
elements = {'Hydrogen': 'H', 'Helium': 'He', 'Lithium': 'Li'}

新增或更新字典项

elements['Carbon'] = 'C'  # Adds 'Carbon' or updates its value to 'C'

移除字典项

del elements['Lithium']  # Removes the key 'Lithium' and its value

检查Key是否存在

if 'Helium' in elements:print('Helium is present')

遍历字典的Key

for element in elements:print(element)  # Prints each key

遍历字典的Value

for symbol in elements.values():print(symbol)  # Prints each value

遍历字典项

for element, symbol in elements.items():print(f'{element}: {symbol}')

字典推导

# Squares of numbers from 0 to 4
squares = {x: x**2 for x in range(5)}

字典合并

合并多个字典

alchemists = {'Paracelsus': 'Mercury'}
philosophers = {'Plato': 'Aether'}
merged = {**alchemists, **philosophers}  # Python 3.5+

带默认值获取字典值

安全的获取字典值,如果不存在,就取默认值

element = elements.get('Neon', 'Unknown')  # Returns 'Unknown' if 'Neon' is not found

操作系统交互

译者注: 这里通常都要引入os包,python自带的,无需安装

路径创建和解析

python处理了系统间的差异,可以安全的创建和解析路径

import os
# Craft a path compatible with the underlying OS
path = os.path.join('mystic', 'forest', 'artifact.txt')
# Retrieve the tome's directory
directory = os.path.dirname(path)
# Unveil the artifact's name
artifact_name = os.path.basename(path)

列举目录内容

import os
contents = os.listdir('enchanted_grove')
print(contents)

创建目录

import os
# create a single directory
os.mkdir('alchemy_lab')
# create a hierarchy of directories
os.makedirs('alchemy_lab/potions/elixirs')

移除文件或者目录

import os
# remove a file
os.remove('unnecessary_scroll.txt')
# remove an empty directory
os.rmdir('abandoned_hut')
# remove a directory and its contents
import shutil
shutil.rmtree('cursed_cavern')

执行shell命令

执行外部的shell命令来增强python的功能

import subprocess
# Invoke the 'echo' incantation
result = subprocess.run(['echo', 'Revealing the arcane'], capture_output=True, text=True)
print(result.stdout)

环境变量交互

import os
# Read the 'PATH' variable
path = os.environ.get('PATH')
# Create a new environment variable
os.environ['MAGIC'] = 'Arcane'

切换当前工作目录

import os
# Traverse to the 'arcane_library' directory
os.chdir('arcane_library')

判断路径是否存在以及路径的文件类型

import os
# Check if a path exists
exists = os.path.exists('mysterious_ruins')
# Ascertain if the path is a directory
is_directory = os.path.isdir('mysterious_ruins')
# Determine if the path is a file
is_file = os.path.isfile('ancient_manuscript.txt')

使用临时文件

import tempfile
# Create a temporary file
temp_file = tempfile.NamedTemporaryFile(delete=False)
print(temp_file.name)
# Erect a temporary directory
temp_dir = tempfile.TemporaryDirectory()
print(temp_dir.name)

获取系统信息

获取主机、系统以及其它信息

import os
import platform
# Discover the operating system
os_name = os.name  # 'posix', 'nt', 'java'
# Unearth detailed system information
system_info = platform.system()  # 'Linux', 'Windows', 'Darwin'

命令行交互 – STDIN,SRDOUT,STDERR

读取用户输入

STDIN读取用户输入

user_input = input("Impart your wisdom: ")
print(f"You shared: {user_input}")

打印到STDOUT

把信息打印到控制台

译者注: 这里应该是打印到stdout,通常stdout是定向到控制台,但是也有可能被重定向到别的地方,例如文件系统

print("Behold, the message of the ancients!")

格式化输出

name = "Merlin"
age = 300
print(f"{name}, of {age} years, speaks of forgotten lore.")

从STDIN逐行读取

import sys
for line in sys.stdin:print(f"Echo from the void: {line.strip()}")

输出到STDERR

输出消息到STDERR

import sys
sys.stderr.write("Beware! The path is fraught with peril.\n")

重定向STDOUT

import sys
original_stdout = sys.stdout  # Preserve the original STDOUT
with open('mystic_log.txt', 'w') as f:sys.stdout = f  # Redirect STDOUT to a fileprint("This message is inscribed within the mystic_log.txt.")
sys.stdout = original_stdout  # Restore STDOUT to its original glory

重定向STDERR

import sys
with open('warnings.txt', 'w') as f:sys.stderr = f  # Redirect STDERRprint("This warning is sealed within warnings.txt.", file=sys.stderr)

控制台读取密码

import getpass
secret_spell = getpass.getpass("Whisper the secret spell: ")

命令行参数

处理和解析命令行参数

import sys
# The script's name is the first argument, followed by those passed by the invoker
script, first_arg, second_arg = sys.argv
print(f"Invoked with the sacred tokens: {first_arg} and {second_arg}")

Argparse实现复杂命令行交互

import argparse
parser = argparse.ArgumentParser(description="Invoke the ancient scripts.")
parser.add_argument('spell', help="The spell to cast")
parser.add_argument('--power', type=int, help="The power level of the spell")
args = parser.parse_args()
print(f"Casting {args.spell} with power {args.power}")

使用Decorator

译者注:在Python中,装饰器(Decorator)是一种用来修改函数或类的功能的工具。装饰器可以在不修改原始函数或类定义的情况下,动态地添加额外的功能或修改其行为。装饰器可以被用来实现日志记录、性能分析、权限检查等功能。它们通常以@decorator_name的语法应用在函数或类定义的上方。

简单Decorator

def my_decorator(func):def wrapper():print("Something is happening before the function is called.")func()print("Something is happening after the function is called.")return wrapper@my_decorator
def say_hello():print("Hello!")say_hello()

带参数的Decorator

def my_decorator(func):def wrapper(*args, **kwargs):print("Before call")result = func(*args, **kwargs)print("After call")return resultreturn wrapper@my_decorator
def greet(name):print(f"Hello {name}")greet("Alice")

使用functools.wraps

在装饰函数时保留原始函数的元数据:

from functools import wrapsdef my_decorator(func):@wraps(func)def wrapper(*args, **kwargs):"""Wrapper function"""return func(*args, **kwargs)return wrapper@my_decorator
def greet(name):"""Greet someone"""print(f"Hello {name}")print(greet.__name__)  # Outputs: 'greet'
print(greet.__doc__)   # Outputs: 'Greet someone'

Class Decorator

class MyDecorator:def __init__(self, func):self.func = funcdef __call__(self, *args, **kwargs):print("Before call")self.func(*args, **kwargs)print("After call")@MyDecorator
def greet(name):print(f"Hello {name}")greet("Alice")

带参数的Decorator

def repeat(times):def decorator(func):@wraps(func)def wrapper(*args, **kwargs):for _ in range(times):func(*args, **kwargs)return wrapperreturn decorator@repeat(3)
def say_hello():print("Hello")say_hello()

Method Decorator

给类中的方法应用decorator

def method_decorator(func):@wraps(func)def wrapper(self, *args, **kwargs):print("Method Decorator")return func(self, *args, **kwargs)return wrapperclass MyClass:@method_decoratordef greet(self, name):print(f"Hello {name}")obj = MyClass()
obj.greet("Alice")

堆叠Decorator

在方法上使用多个decorator

@my_decorator
@repeat(2)
def greet(name):print(f"Hello {name}")greet("Alice")

Decorator使用可选参数

def smart_decorator(arg=None):def decorator(func):@wraps(func)def wrapper(*args, **kwargs):if arg:print(f"Argument: {arg}")return func(*args, **kwargs)return wrapperif callable(arg):return decorator(arg)return decorator@smart_decorator
def no_args():print("No args")@smart_decorator("With args")
def with_args():print("With args")no_args()
with_args()

类方法Decorator

class MyClass:@classmethod@my_decoratordef class_method(cls):print("Class method called")MyClass.class_method()

静态方法Decorator

class MyClass:@staticmethod@my_decoratordef static_method():print("Static method called")MyClass.static_method()

字符串操作

字符串拼接

greeting = "Hello"
name = "Alice"
message = greeting + ", " + name + "!"
print(message)

字符串格式化

message = "{}, {}. Welcome!".format(greeting, name)
print(message)

字符串大小写转换

s = "Python"
print(s.upper())  # Uppercase
print(s.lower())  # Lowercase
print(s.title())  # Title Case

字符串操作 strip,rstrip,lstrip

s = "   trim me   "
print(s.strip())   # Both ends
print(s.rstrip())  # Right end
print(s.lstrip())  # Left end

字符串操作 startswith,endswith

s = "filename.txt"
print(s.startswith("file"))  # True
print(s.endswith(".txt"))    # True

字符串操作 split,join

s = "split,this,string"
words = s.split(",")        # Split string into list
joined = " ".join(words)    # Join list into string
print(words)
print(joined)

字符串操作 replace

s = "Hello world"
new_s = s.replace("world", "Python")
print(new_s)

字符串操作find,index

s = "look for a substring"
position = s.find("substring")  # Returns -1 if not found
index = s.index("substring")    # Raises ValueError if not found
print(position)
print(index)

字符串操作isdigit,isalpha,isalnum

print("123".isdigit())   # True
print("abc".isalpha())   # True
print("abc123".isalnum())# True

字符串切片

s = "slice me"
sub = s[2:7]  # From 3rd to 7th character
print(sub)

这篇关于终极Python备忘单:日常任务的实用Python的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1068096

相关文章

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import

Python Jupyter Notebook导包报错问题及解决

《PythonJupyterNotebook导包报错问题及解决》在conda环境中安装包后,JupyterNotebook导入时出现ImportError,可能是由于包版本不对应或版本太高,解决方... 目录问题解决方法重新安装Jupyter NoteBook 更改Kernel总结问题在conda上安装了

Python如何计算两个不同类型列表的相似度

《Python如何计算两个不同类型列表的相似度》在编程中,经常需要比较两个列表的相似度,尤其是当这两个列表包含不同类型的元素时,下面小编就来讲讲如何使用Python计算两个不同类型列表的相似度吧... 目录摘要引言数字类型相似度欧几里得距离曼哈顿距离字符串类型相似度Levenshtein距离Jaccard相

Python安装时常见报错以及解决方案

《Python安装时常见报错以及解决方案》:本文主要介绍在安装Python、配置环境变量、使用pip以及运行Python脚本时常见的错误及其解决方案,文中介绍的非常详细,需要的朋友可以参考下... 目录一、安装 python 时常见报错及解决方案(一)安装包下载失败(二)权限不足二、配置环境变量时常见报错及

Python中顺序结构和循环结构示例代码

《Python中顺序结构和循环结构示例代码》:本文主要介绍Python中的条件语句和循环语句,条件语句用于根据条件执行不同的代码块,循环语句用于重复执行一段代码,文章还详细说明了range函数的使... 目录一、条件语句(1)条件语句的定义(2)条件语句的语法(a)单分支 if(b)双分支 if-else(

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详

Java深度学习库DJL实现Python的NumPy方式

《Java深度学习库DJL实现Python的NumPy方式》本文介绍了DJL库的背景和基本功能,包括NDArray的创建、数学运算、数据获取和设置等,同时,还展示了如何使用NDArray进行数据预处理... 目录1 NDArray 的背景介绍1.1 架构2 JavaDJL使用2.1 安装DJL2.2 基本操