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()

相关文章
|
22天前
|
存储 算法 调度
【复现】【遗传算法】考虑储能和可再生能源消纳责任制的售电公司购售电策略(Python代码实现)
【复现】【遗传算法】考虑储能和可再生能源消纳责任制的售电公司购售电策略(Python代码实现)
128 26
|
11天前
|
测试技术 Python
Python装饰器:为你的代码施展“魔法”
Python装饰器:为你的代码施展“魔法”
196 100
|
11天前
|
开发者 Python
Python列表推导式:一行代码的艺术与力量
Python列表推导式:一行代码的艺术与力量
202 95
|
19天前
|
Python
Python的简洁之道:5个让代码更优雅的技巧
Python的简洁之道:5个让代码更优雅的技巧
174 104
|
19天前
|
开发者 Python
Python神技:用列表推导式让你的代码更优雅
Python神技:用列表推导式让你的代码更优雅
304 99
|
11天前
|
缓存 Python
Python装饰器:为你的代码施展“魔法
Python装饰器:为你的代码施展“魔法
129 88
|
16天前
|
监控 机器人 编译器
如何将python代码打包成exe文件---PyInstaller打包之神
PyInstaller可将Python程序打包为独立可执行文件,无需用户安装Python环境。它自动分析代码依赖,整合解释器、库及资源,支持一键生成exe,方便分发。使用pip安装后,通过简单命令即可完成打包,适合各类项目部署。
|
19天前
|
设计模式 人工智能 API
AI智能体开发实战:17种核心架构模式详解与Python代码实现
本文系统解析17种智能体架构设计模式,涵盖多智能体协作、思维树、反思优化与工具调用等核心范式,结合LangChain与LangGraph实现代码工作流,并通过真实案例验证效果,助力构建高效AI系统。
239 7
|
24天前
|
存储 大数据 Unix
Python生成器 vs 迭代器:从内存到代码的深度解析
在Python中,处理大数据或无限序列时,迭代器与生成器可避免内存溢出。迭代器通过`__iter__`和`__next__`手动实现,控制灵活;生成器用`yield`自动实现,代码简洁、内存高效。生成器适合大文件读取、惰性计算等场景,是性能优化的关键工具。
177 2
|
21天前
|
JSON 缓存 开发者
淘宝商品详情接口(item_get)企业级全解析:参数配置、签名机制与 Python 代码实战
本文详解淘宝开放平台taobao.item_get接口对接全流程,涵盖参数配置、MD5签名生成、Python企业级代码实现及高频问题排查,提供可落地的实战方案,助你高效稳定获取商品数据。

推荐镜像

更多