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

相关文章
|
2天前
|
数据处理 Python
从零开始学迭代器生成器:打造高效、易读的Python代码
从零开始学迭代器生成器:打造高效、易读的Python代码
|
3天前
|
机器学习/深度学习 算法 索引
Python常用极简代码
Python常用极简代码
28 5
|
1天前
|
存储 数据库 Python
Python 脚本死锁问题与解决方案
该 Python 脚本旨在启动多个线程,每个线程又通过 Popen 启动一个子进程。子进程将从一个数据库中的表格中将 10M 条记录传输到另一个数据库中的不同表格中。这个过程中会涉及大量的数据整理和转换,因为两个数据库具有不同的架构。子进程在执行过程中,如果遇到错误(如错误的记录或重复的主键)或执行成功,都会输出 “Done\n”;如果没有更多记录可供传输,则会输出 “NO DATA\n”。
|
3天前
|
Python
Python实用案例代码详解
Python实用案例代码详解
12 2
|
1天前
|
Shell Python
技术经验解读:使用python脚本传递参数:(三种方式可收藏)
技术经验解读:使用python脚本传递参数:(三种方式可收藏)
|
1天前
|
程序员 API 计算机视觉
技术经验解读:【python自动化】02.pywin32库自动操作键鼠(保姆级代码注释)
技术经验解读:【python自动化】02.pywin32库自动操作键鼠(保姆级代码注释)
|
2天前
|
数据安全/隐私保护 Python
程序技术好文:猪圈密码python脚本实现
程序技术好文:猪圈密码python脚本实现
10 0
|
2天前
|
人工智能 数据挖掘 大数据
538个代码示例!麻省理工教授的Python程序设计+人工智能案例实践
Python简单易学,且提供了丰富的第三方库,可以用较少的代码完成较多的工作,使开发者能够专注于如何解决问题而只花较少的时间去考虑如何编程。 此外,Python还具有免费开源、跨平台、面向对象、胶水语言等优点,在系统编程、图形界面开发、科学计算、Web开发、数据分析、人工智能等方面有广泛应用。 尤其是在数据分析和人工智能方面,Python已成为最受开发者欢迎的编程语言之一,不仅大量计算机专业人员选择使用Python进行快速开发,许多非计算机专业人员也纷纷选择Python语言来解决专业问题。 由于Python应用广泛,关于Python的参考书目前已经有很多,但将Python编程与数据分析、人工智
|
1月前
|
存储 Unix 编译器
汇编语言----X86汇编指令
汇编语言----X86汇编指令
41 1
|
1月前
|
存储 机器学习/深度学习 移动开发
汇编语言指令系列
汇编语言指令系列
120 0