1. 常用的Python模块集锦
- 时间相关
time
模块
time.sleep(seconds)
- 系统相关
sys
- I2C总线
smbus2
- 串口相关
pyserial
- 参数检查
validator
- 数据打包
struct
- 队列
Queue
https://www.cnblogs.com/itogo/p/5635629.html - 文件及路径相关操作
os
https://www.cnblogs.com/yufeihlf/p/6179547.html
os.path.exists(filename)
os.remove(filename)
os.getcwd()
2. 字符串转与ASCII码互转
利用python內嵌的ord()
函數
data = map(ord, list(string))
利用Python的chr
函数
data = map(chr, datalist)
3. 函數參數檢查
利用validator模块
利用Python内嵌的
isinstance()
函数只能检查参数类型是否正确。
4. 数据打包模块struct
打包:struct.pack(fmt, v1, v2, …)
解包:struct.unpack(fmt, buffer)
5.Python注意事项
- repr是原始字符串函数,打印出来的字符串不会被转译。
>>>print(repr("hello,\nworld!"))
'hello, \nworld!'
- 原始字符串用前缀r表示,但是不能以反斜杠结尾。
>>>print(r'C:\Program Files\fnord\foo')
C:\Program Files\fnord\foo
- 可使用前缀b直接创建bytes对象(而不是字符串)。
>>>b'\x00hello world!\x0a'
b'\x00hello world!\x0a'