3. 引用module时遇到的问题
- 用pycharm写代码时,使用包下面的module时会报错说找不到对应的module >>> 右键包-Mark Directory as - Source Root
Python中操作Excel
使用第三方python库openpyxl
pip install openpyxl
# 引入库
from openpyxl import load_workbook
# 加载一个excel文件,进入到工作簿
wb = load_workbook("testdata.xlsx")
# 获取指定的表单
sh = wb["Sheet1"]
# 获取表单中数据
name = sh.cell(row=2, column=1).value
print(name)
# 修改表单中的数据--修改数据,注意在写操作时,必须先关闭excel文件
sh.cell(row=2, column=3).value = 18
# 保存修改数据的操作--保存数据
wb.save("testdata.xlsx")
# 获取总行数、总列数
print(sh.max_row)
print(sh.max_column)
print(sh.cell(row=3, column=3).value)
sh.cell(row=3, column=3).value = 25
print(sh.cell(row=3, column=3))
# 读取所有数据,按行读取
for index in range(1, sh.max_row + 1):
print("行号: ", index)
for sub_i in range(1, sh.max_column + 1):
print("列号: ", sub_i, "内容: ", sh.cell(row=index, column=sub_i).value)
View Code
Python包管理工具-pip
1. pip install xx
2. 可以把需要安装的包写在一个文件里面requirements.txt, 然后一起安装 pip install -r requirements.txt
Python编码
本来期望结果输出的是中文,结果来一段像这样xe4xbdxa0xe5xa5xbd
像是乱码的字符串,就要进行解码decode
如果输出是一串乱码,就要先编码encode,然后在进行解码
u = "中文"
str1 = u.encode('gb2312') # 以gb2312编码对u进行编码,获得bytes类型对象
print(str1) # b'xd6xd0xcexc4'
str2 = u.encode('gbk') # 以gbk编码对u进行编码,获得bytes类型对象
print(str2) # b'xd6xd0xcexc4'
str3 = u.encode('utf-8') #以utf-8编码对u进行编码,获得bytes类型对象
print(str3) # b'xe4xb8xadxe6x96x87'
u3 = str3.decode('utf-8')
print(u3)
u1 = str1.decode('utf-8')
print(u1) # UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd6 in position 0: invalid continuation byte
Python获取命令行参数
import sys
print('The command line arguments are:')
for i in sys.argv:
print(i) # 第一个参数是文件的路径
# run command "python test2/f1_module_using_sys.py we are arguments"
# results: >>>>>>
# pythonBasicLearnf1_module_using_sys.py
# we
# are
# argument
Python问题
1. 动态获取module里面的属性 self.request_path = getattr(path, attr)
2. list转化成逗号相连的字符串 ",".join(map(str, leads_id))
3. json(字符串)转化成字典 josn.loads(str) '{"key": "value"}'->{"key": "value"}
字典转化成json(字符串) json.dumps(dict)