python 几个小功能

栏目: Python · 发布时间: 6年前

内容简介:## pandas 默认读取第一个工作簿```import pandas as pd

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 彩蛋

# XKCD的漫画
import antigravity

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

游戏化思维

游戏化思维

[美] 凯文·韦巴赫(Kevin Werbach)、[美] 丹·亨特(Dan Hunter) / 周逵、王晓丹 / 浙江人民出版社 / 2014-4 / 36.90

[内容简介] ●本书由开设了全世界第一个游戏化课程的沃顿商学院副教授凯文·韦巴赫和丹·亨特所著,第一次全面系统地介绍游戏化的理论,阐述了如何将游戏的理念应用到商业实践中。 ●作者指出,在商业竞争日益激烈的今天,传统的激励方式渐渐失效,未来的管理将更多地建立在员工和消费者的内在动机和自我激励上。这些制作精良、设计巧妙的游戏建立在多年来对人类动机和人类心理的研究基础之上,可以最大限度地激发......一起来看看 《游戏化思维》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具