python的这几个小功能,你都会用了吗?

364 篇文章 14 订阅
362 篇文章 18 订阅

image

python 随机抽取

import random
country = ['北京', '上海', '重庆', '哈尔滨', '广州', '海南']
target = random.choice(country)
target3 = random.sample(country, 3)
print(f'地点: {target}')
print(f'地点: {target3}')

python 冒泡排序

lis = [56, 12, 1,8, 354, 10, 100,34,56,7,23,456,234,-58]
def sortport():
 for i in range(len(lis)-1):
 for j in range(len(lis)-1-i):
 if lis[j] > lis[j+1]:
 lis[j], lis[j+1] = lis[j+1], lis[j]
 return lis
print(sortport())

列出当前目录下所有的文件和目录名

import os
print([d for d in os.listdir('.')])

python 生成随机验证码

import random, string
str1 = '0123456789'
str2 = string.ascii_letters # 包含所有字母大小写的字符串
str3 = str1 + str2
ma1 = random.sample(str3, 6)
ma1 = ''.join(ma1)
print(ma1)

检查是否只由数字组成

chri = '12345'
print(chri.isdigit()) 
print(chri.isnumeric()) # 这个只针对unicode对象
    ## pandas 默认读取第一个工作簿
    import pandas as pd
    df = pd.read_excel('xxx.xlsx')
    import pandas as pd

默认读取第一个工作簿

import pandas as pd
df = pd.read_excel('xxx.xlsx')

读取所有工作簿

import pandas as pd
df_dict = pd.read_excel('xxxx.xlsx', sheet_name=None)
for sheet_name, df in df_dict.item():
 print(f'工作簿的名字为:{sheet_name}')
 print(df.head())

使用Python解压zip文件:

import zipfile
zip_ref = zipfile.ZipFile(path_to_zip_file, 'r')
zip_ref.extractall(directory_to_extract_to)
zip_ref.close()

在Python中复制文件

import shutil
shutil.copy('源文件路径', '目标路径')

在Python Selenium + Chromedriver中自定义缓存路径

import os
from selenium import webdriver
os.makedirs('cache', exist_ok=True)
options = webdriver.ChromeOptions()
options.add_argument('--disk-cache-dir=./cache')
driver = webdriver.Chrome('./chromedriver', options=options)

在学习Python的道路上,经常会碰到许多的问题,而我们在一起问题就不是问题了,可以在小编找到挪威哒,一起学习,也可以私信“01”获取学习干货,碰到什么问题也可以及时来问小编哒。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值