简单判断语句的缩写
if len(content) > 15:
title=content[0:15]
else:
title = content
可以简化为
##结果甲 if 条件甲 else 结果乙 if 条件乙 else 结果丙
title = content[0:15] if len(content)>15 else content```
匿名函数
lambda
lambda 参数:函数的内容和返回的值
(lambda x,y: x if x > y else y)(33,59)
##59
获取文件的绝对路径
##获取到当前文件所在的绝对路径
path = os.path.abspath(os.path.dirname(sys.argv[0]))
###结果 到项目名的目录下
##加上文件的路径
path = path + r"\douyin.html"
##获取当前路径
path = os.getcwd()
for 循环 生成列表简写 列表循环
#前
list1 = []
for i in range(10):
list1.append(i)
print(list1)
# 后
print([i for i in range(10)])
#结果
#[0,1,2,3,4,5,6,7,8,9]
# 前
url = 'https://xxxx.xx.com/page=1'
list_url = []
for i in range(1,10):
url = 'https://xxxx.xx.com/page={}'.format(i)
list_url.append(url)
print(list_url)
#后
urls = [
f'https://xxxx.xx.com/page={i}'
for i in range(10)
]
print(urls)
# 结果
#['https://xxxx.xx.com/page=0', 'https://xxxx.xx.com/page=1', 'https://xxxx.xx.com/page=2', 'https://xxxx.xx.com/page=3',。。。。
参考
https://blog.csdn.net/qq_36759224/article/details/104724999
记录一个不确定使用什么方法的简易解决方法
前情提要:使用hashlib的方法是数据返回的方法 方法可能是sha256 可能是sha1等
解决:
def call_method(method_name, str1):
#getattr()来获取对应的方法
#__main__ 模块作为要搜索方法的对象。如果你的方法定义在其他模块中,需要将相应的模块对象传递给getattr()函数。
# 我需要hashlib的方法所以 __main__ 替换成hashlib
#str1:要调用的方法需要传入的参数
method = getattr(__main__, method_name, None)
if method:
return method(str1)
else:
print("找不到对应的方法")
if __name__ == "__main__":
hashstr = call_method("sha1", "你好".encode("utf-8")).hexdigest()