随笔分类 - python杂技
摘要:使用PyInstaller打包后,运行exe程序会自动解压到C盘虚拟空间进行运行,导致读取读取不到实际文件位置 解决方法: 在脚本里设置运行路径 #脚本运行目录设置 # 如果程序被打包为可执行文件 if getattr(sys, 'frozen', False): # 获取可执行文件所在的目录 BA
阅读全文
摘要:背景 工作中需要一个测试需求:需要比对两个excel文件的内容,以门店编码为唯一键,比对其他字段值不一致的地方,如有不一致需要写入另外一个文件 解决方案 使用python代码实现 # -*- coding: utf-8 -*- """ @File : 数据核对脚本.py @Author : simo
阅读全文
摘要:1、爬取某网站内容时,返回的结果为乱码,如图: 2、写在前面的解释 Requests会基于HTTP头部响应的编码做出有根据的推测,当访问r.text时,Requests会使用其推测的文本编码。 查看网页返回的字符集类型:r.apparent_encoding 查看自动判断的字符集类型:r.encod
阅读全文
摘要:import random def random_20char(length): #定义一个空列表 string=[] for i in range(length): #生成一个随机值需要转换成字符串 x = str(random.randint(0, 9)) #包含数字和字符 # if x ==
阅读全文
摘要:第一种 import sys import time def progress(percent,width=50): '''进度打印功能''' if percent >= 100: percent=100 show_str=('[%%-%ds]' %width) %(int(width * perc
阅读全文
摘要:获取指定日期前后N天日期: 方法:字符串转化为日期格式后,进行加减,然后再转化为字符串 import datetime from datetime import timedelta # 字符串转化为日期格式 target_date = datetime.datetime.strptime('2019
阅读全文
摘要:#手机号码校验 import re while True: iphone = input('请输入手机号码:').strip() res = re.match("1[35678]\d{9}", iphone) #res = re.match("1[3-9]\d{9}", iphone) if res
阅读全文
摘要:1 正则表达式相关 当给你一大堆文本信息,让你提取其中的指定数据时,可以使用正则来实现。例如:提取文本中的邮箱和手机号 import re text = "楼主太牛逼了,在线想要 442662578@qq.com和xxxxx@live.com谢谢楼主,手机号也可15131255789,搞起来呀" p
阅读全文
摘要:登录接口 user = {'user': None} def login_execute(): while True: print("欢迎使用登录功能".center(50, '*')) username = input('请输入用户名(Q/q退出):').strip() if username.u
阅读全文
摘要:from src.service import download, search, page def run(): func_dict = { '1': {'title': "分页看新闻", 'func': page.execute}, '2': {'title': "搜索专区", 'func':
阅读全文
摘要:def view_per_data(page_num, per_page_data): ''' 获取指定页码的数据 :param page_num: 页码 :param per_page_data: 每页数据 :return: ''' page_num = int(page_num) per_pag
阅读全文