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

发布于:2024-06-17 ⋅ 阅读:(16) ⋅ 点赞:(0)

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

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

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

文章目录

概述

这个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, 5xx
    data = 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 seconds
    data = 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 file
    print("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 STDERR
    print("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 result
    return wrapper

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

greet("Alice")

使用functools.wraps

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

from functools import wraps

def 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 = func
   def __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 wrapper
    return 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 wrapper

class MyClass:
    @method_decorator
    def 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 wrapper
    if 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_decorator
    def class_method(cls):
        print("Class method called")

MyClass.class_method()

静态方法Decorator

class MyClass:
    @staticmethod
    @my_decorator
    def 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)