This project is a curated collection of Python tips and tricks, designed to help both beginners and experienced developers discover useful techniques and solve common problems more efficiently. Whether you're learning Python or looking for a quick reminder on a specific syntax, this repository aims to be a helpful resource.
本专案收集整理了一些 Python 的实用技巧和窍门,旨在帮助初学者和有经验的开发者学习实用技巧,更高效地解决常见问题。无论您是正在学习 Python,还是想快速回顾某个特定语法,本代码仓库都希望能为您提供有用的参考。
The tips are organized within the code/ directory. Each tip is contained in a separate Python file, named with a number and a descriptive title in Chinese. This naming convention helps in quickly identifying the content of each script. For example, 001-一行写if-else.py demonstrates how to write an if-else statement in a single line.
这些技巧被整理在 code/ 目录下。每个技巧都包含在一个独立的 Python 文件中,文件名以数字和中文描述性标题命名。这种命名约定有助于快速识别每个脚本的内容。例如,001-一行写if-else.py 演示了如何在一行中编写 if-else 语句。
You can explore the Python tips in two main ways:
- Browse the
code/directory: Navigate directly into thecode/folder to see all the individual Python scripts. - Use the Table of Contents: The expandable "目录 Content" section below lists all available tips. Click on it to see the full list and then refer to the corresponding file in the
code/directory.
Each script is self-contained and demonstrates a specific tip or trick.
您可以通过以下两种主要方式来查阅这些 Python 技巧:
- 浏览
code/目录: 直接进入code/文件夹查看所有独立的 Python 脚本。 - 使用目录: 下方可展开的 “目录 Content” 部分列出了所有可用的技巧。点击它可以查看完整列表,然后参考
code/目录中对应的文件。
每个脚本都是独立,并演示一个特定的技巧或窍门。
📠 目录 Content
- 001-一行写if-else
- 002-提取字典的key 和 value
- 003-分割字符-从右边开始rsplit
- 004-在csv中写入固定数据-字典-行-表头-解决空行
- 005-爬虫-关闭安全验证verify和安全警告
- 006-爬虫-中文乱码的解决方法
- 007-保留几位小数
- 008-join拼接列表成字符串-即split用法
- 009-循环遍历两个列表
- 010-去掉左右多余的字符-strip
- 011-csv写入中文乱码-utf-8-sig
- 012-一行打印-还能进行分行显示
- 013-特殊字符不转义加r
- 014-BeautifulSoup-基本语法
- 015-多线程基本方法
- 016-关于时间time转换
- 017-webrowser自动打开系统默认浏览器(自带库)
- 018-把数转成二进制或者其他进制
- 019-xpath新知识-模糊匹配-大于第几个的position-通过文本定位
- 020-格式化输出f-string字符串-居中等
- 021-函数的参数类型注释-和返回注释
- 022-将列表或元组或字典中的多个元素进行函数传参
- 023-函数定长和不定长参数
- 024-匿名函数lamda基本方法
- 025-打印进度条-不换行(原位打印)-从行首打印反斜杠r
- 026-格式化字符串format的另一种用法
- 027-字符串转base64编码-解码
- 028-判断文件夹是否存在,否则创建
- 029-二维码的创建-基本方法
- 030-文件分批写入-以免文件一次写入过大
- 031-列出某一类文件(后缀)-列表
- 032-sqlite3数据库-基础方法
- 033-从其他文件调用
- 034-csv表格基本方法
- 035-两个列表同时循环遍历-zip
- 036-print()输出到文件
- 037-python-md5-sha256加密
- 038-用0填充某个字符串的长度zfill_数字补0
- 039-用特定字符填充-rjust
- 040-setcookie请求时添加cookies-session
- 041-列表倒序-打印索引值
- 042-自带库hmac-实现MD5加密
- 043-pyauto基本-按键自动点击-鼠标位置等
- 044-自带库winsound-提示音
- 045-从其他文件引入变量
- 046-从遍历列表运行函数
- 047-函数装饰器基本
- 048-回调函数-传参基本
- 049-多线程示例
- 050-文件路径不存在-就创建os_path_exist
- 051-合并两个字典
- 052-控制台打印不同颜色字符
- 053-python执行cmd命令两种方法
- 054-f格式化字符串-遇到本来就有小括号{问题
- 055-查看文件修改的时间日期
Contributions are welcome! If you have a Python tip or trick you'd like to share, please feel free to:
- Fork the repository.
- Create a new
.pyfile in thecode/directory. Please follow the existing naming convention (e.g.,NNN-your-tip-description.py, where NNN is the next available number). - Add a brief description of your tip, including the Chinese filename, to the table of contents in this README.
- Submit a pull request.
欢迎投稿!如果您有想要分享的 Python 技巧或窍门,请随时:
- Fork 本代码仓库。
- 在
code/目录下创建一个新的.py文件。请遵循现有的命名约定(例如NNN-你的技巧描述.py,其中 NNN 是下一个可用的编号)。 - 将您的技巧的简要说明(包括中文文件名)添加到此 README 的目录中。
- 提交拉取请求。