python 代码脚本汇编

简介: python 代码脚本汇编

if / else 三目运算

复制代码
age = 19
if age > 18:
ji = "adult"
else:
ji = "child"
ji = 'adult' if age > 18 else 'child' //这样写直接直观
print ji

真值判断
if attr == True:
do_something()
if len(values) != 0: # 判断列表是否为空
可以直接这样写
if attr:
if values:
for / else break语句不执行时候就会执行 else语句
age = [3,6,8,2,7,8,4,67,3]
res = False
for i in age:
if i > 60:
res = True
break
if not res:
print "noooo"
print "yessssss"
//使用 for / else 进行判断
print "yesssssss"
print "nooooooooo"
获取字典元素
dic = {"name":"jk","age":23}
if dic.has_key("name"):
print dic['name']
print "no name attr"
print dic.get("name","no name attr")
文件内容的对比
符号含义的说明
'-' 包含在第一个序列行中,但不包含在第二个序列行
'+' 包含在第二个序列行中,但不包含在第一个序列行
'' 两个序列行一致
'?'标志两个序列行存在增量差异
'^' 标志出两个 序列行存在的差异字符
import difflib
test1="""
helloworld232323
33333333
6666
444444444
"""
test2="""
hellowerqerqererq
helli
44444444445
test1_t=test1.splitlines()
test2_t=test2.splitlines()

打印出内容的不同

diff=difflib.Differ()
diff_cont=diff.compare(test1_t,test2_t)
print "\n".join(list(diff_cont))

生成HTML 文档格式

diff=difflib.HtmlDiff()
print diff.make_file(test1_t,test2_t)
//代码效果参考:http://www.ezhiqi.com/bx/art_2735.html

遍历路径下的文件/目录
1 使用os.lisdir递归
dirlist=[]
filelist=[]
def listall(dir):
for files in os.listdir(dir):
dir_file_path = os.path.join(dir,files)
if os.path.isdir(dir_file_path):
dirlist.append(dir_file_path)
listall(dir_file_path)
else:
filelist.append(dir_file_path)
listall('./')
print dirlist
print filelist
2 使用os.walk()
for root,dirs,files in os.walk('./'):
for dir in dirs:
dirlist.append(os.path.join(root,dir))
for file in files:

    filelist.append(os.path.join(root,file))

requests / urllib2 两种方法http请求
import requests
import urllib2
url="http://www.nipic.com/"
print requests.get(url).content
print urllib2.urlopen(url).read()
使用lxml 模块解析html页面中的所有img元素下载到本地
import urllib.request
import os
from lxml import html
def main():

opens xkcd.com

try:
    page = requests.get("http://www.nipic.com/")
except requests.exceptions.RequestException as e:
    print(e)
    exit()
# parses xkcd.com page
tree = html.fromstring(page.content)
# finds image src url
image_src = tree.xpath("//img/@src")
#//B[@id] 
#所有具有属性id的B元素 
comic_location_dir = os.getcwd() + '/comics/'
# checks if save location exists else creates
# if not os.path.exists(comic_location_dir):
#     os.makedirs(comic_location)
num=0
for x in image_src:
    comic_location = comic_location_dir + str(num) + ".jpg"
    print x
    print comic_location
    urllib.request.urlretrieve(x, comic_location)
    num = num + 1

//代码效果参考:http://www.ezhiqi.com/bx/art_7343.html

if name == "main":
main()

相关文章
|
16天前
|
缓存 JSON API
VIN车辆识别码查询车五项 API 实践指南:让每一俩车有迹可循(Python代码示例)
VIN(车辆识别代码)是全球唯一的17位汽车标识码,可快速获取车架号、发动机号、品牌型号等核心信息。在二手车交易、保险理赔、维修保养等场景中,准确解析VIN有助于提升效率与风控能力。本文介绍VIN码结构、适用场景,并提供Python调用示例及优化建议,助力企业实现车辆信息自动化核验。
65 1
|
4天前
|
API 数据安全/隐私保护 Python
批量发短信的软件,自动群发短信批量工具,手机号电话生成脚本插件【python】
该工具包含三个核心模块:短信发送核心功能、配置管理系统和命令行界面。使用时需先配置API密钥和短信模板
|
1月前
|
数据采集 机器学习/深度学习 编解码
从零复现Google Veo 3:从数据预处理到视频生成的完整Python代码实现指南
本文详细介绍了一个简化版 Veo 3 文本到视频生成模型的构建过程。首先进行了数据预处理,涵盖了去重、不安全内容过滤、质量合规性检查以及数据标注等环节。
123 5
从零复现Google Veo 3:从数据预处理到视频生成的完整Python代码实现指南
|
6天前
|
JSON 机器人 API
微信机器人自动回复插件,vx自动回复机器人脚本助手,python框架分享
这个微信机器人系统包含三个主要模块:主程序基于itchat实现微信消息监听和自动回复功能
|
9天前
|
数据采集 存储 数据库
Python爬虫开发:Cookie池与定期清除的代码实现
Python爬虫开发:Cookie池与定期清除的代码实现
|
12天前
|
数据安全/隐私保护 Python
|
1月前
|
机器学习/深度学习 人工智能 PyTorch
200行python代码实现从Bigram模型到LLM
本文从零基础出发,逐步实现了一个类似GPT的Transformer模型。首先通过Bigram模型生成诗词,接着加入Positional Encoding实现位置信息编码,再引入Single Head Self-Attention机制计算token间的关系,并扩展到Multi-Head Self-Attention以增强表现力。随后添加FeedForward、Block结构、残差连接(Residual Connection)、投影(Projection)、层归一化(Layer Normalization)及Dropout等组件,最终调整超参数完成一个6层、6头、384维度的“0.0155B”模型
130 11
200行python代码实现从Bigram模型到LLM
|
8天前
|
API 数据安全/隐私保护 Python
贴吧私信自动群发神器,百度贴吧群发批量私信脚本插件,python框架分享
这个贴吧私信群发工具包含三个主要文件:主程序、配置文件和入口文件。主程序实现了登录
|
8天前
|
API 数据安全/隐私保护 Python
小红书批量发布协议, 抖音自动批量发布软件脚本,笔记作品视频自动发布工具【python】
这个工具框架包含了小红书和抖音的批量发布功能,支持图片和视频处理、定时发布等功能
|
9天前
|
Web App开发 数据安全/隐私保护 Python
快手批量发布作品工具,自动上传视频发布软件,python实现自动脚本
这个脚本实现了快手批量上传视频的功能,包含登录、上传视频、添加描述和发布等完整流程

热门文章

最新文章

推荐镜像

更多