Python语言的12个基础知识点小结


Posted in Python onJuly 10, 2014

python编程中常用的12种基础知识总结:正则表达式替换,遍历目录方法,列表按列排序、去重,字典排序,字典、列表、字符串互转,时间对象操作,命令行参数解析(getopt),print 格式化输出,进制转换,Python调用系统命令或者脚本,Python 读写文件。

1、正则表达式替换

目标: 将字符串line中的 overview.gif 替换成其他字符串

>>> line = '<IMG ALIGN="middle" SRC=\'#\'" />'

>>> mo=re.compile(r'(?<=SRC=)"([\w+\.]+)"',re.I) 
>>> mo.sub(r'"\1****"',line)

'<IMG ALIGN="middle" SRC=\'#\'" /span>
>>> mo.sub(r'replace_str_\1',line)

'<IMG ALIGN="middle" replace_str_overview.gif BORDER="0" ALT="">'< /span>
>>> mo.sub(r'"testetstset"',line)

'<IMG ALIGN="middle" SRC=\'#\'" /span>

注意: 其中 \1 是匹配到的数据,可以通过这样的方式直接引用

2、遍历目录方法

在某些时候,我们需要遍历某个目录找出特定的文件列表,可以通过os.walk方法来遍历,非常方便

import os

fileList = []

rootdir = "/data"

for root, subFolders, files in os.walk(rootdir):

if '.svn' in subFolders: subFolders.remove('.svn')  # 排除特定目录

for file in files:

  if file.find(".t2t") != -1:# 查找特定扩展名的文件

      file_dir_path = os.path.join(root,file)

      fileList.append(file_dir_path) 
print fileList

3、列表按列排序(list sort)

如果列表的每个元素都是一个元组(tuple),我们要根据元组的某列来排序的化,可参考如下方法
下面例子我们是根据元组的第2列和第3列数据来排序的,而且是倒序(reverse=True)

>>> a = [('2011-03-17', '2.26', 6429600, '0.0'), ('2011-03-16', '2.26', 12036900, '-3.0'),

 ('2011-03-15', '2.33', 15615500,'-19.1')]

>>> print a[0][0]

2011-03-17

>>> b = sorted(a, key=lambda result: result[1],reverse=True)

>>> print b

[('2011-03-15', '2.33', 15615500, '-19.1'), ('2011-03-17', '2.26', 6429600, '0.0'),

('2011-03-16', '2.26', 12036900, '-3.0')]

>>> c = sorted(a, key=lambda result: result[2],reverse=True)

>>> print c

[('2011-03-15', '2.33', 15615500, '-19.1'), ('2011-03-16', '2.26', 12036900, '-3.0'),

('2011-03-17', '2.26', 6429600, '0.0')]

4、列表去重(list uniq)

有时候需要将list中重复的元素删除,就要使用如下方法

>>> lst= [(1,'sss'),(2,'fsdf'),(1,'sss'),(3,'fd')]

>>> set(lst)

set([(2, 'fsdf'), (3, 'fd'), (1, 'sss')])

>>>

>>> lst = [1, 1, 3, 4, 4, 5, 6, 7, 6]

>>> set(lst)

set([1, 3, 4, 5, 6, 7])

5、字典排序(dict sort)

一般来说,我们都是根据字典的key来进行排序,但是我们如果想根据字典的value值来排序,就使用如下方法

>>> from operator import itemgetter

>>> aa = {"a":"1","sss":"2","ffdf":'5',"ffff2":'3'}

>>> sort_aa = sorted(aa.items(),key=itemgetter(1))

>>> sort_aa

[('a', '1'), ('sss', '2'), ('ffff2', '3'), ('ffdf', '5')]

6、字典,列表,字符串互转

以下是生成数据库连接字符串,从字典转换到字符串

>>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}

>>> ["%s=%s" % (k, v) for k, v in params.items()]

['server=mpilgrim', 'uid=sa', 'database=master', 'pwd=secret']

>>> ";".join(["%s=%s" % (k, v) for k, v in params.items()])

'server=mpilgrim;uid=sa;database=master;pwd=secret'

下面的例子 是将字符串转化为字典
>>> a = 'server=mpilgrim;uid=sa;database=master;pwd=secret'

>>> aa = {}

>>> for i in a.split(';'):aa[i.split('=',1)[0]] = i.split('=',1)[1]

...

>>> aa

{'pwd': 'secret', 'database': 'master', 'uid': 'sa', 'server': 'mpilgrim'}

7、时间对象操作

将时间对象转换成字符串

>>> import datetime

>>> datetime.datetime.now().strftime("%Y-%m-%d %H:%M")

  '2011-01-20 14:05'

时间大小比较
>>> import time

>>> t1 = time.strptime('2011-01-20 14:05',"%Y-%m-%d %H:%M")

>>> t2 = time.strptime('2011-01-20 16:05',"%Y-%m-%d %H:%M")

>>> t1 > t2

  False

>>> t1 < t2

  True

时间差值计算,计算8小时前的时间
>>> datetime.datetime.now().strftime("%Y-%m-%d %H:%M")

  '2011-01-20 15:02'

>>> (datetime.datetime.now() - datetime.timedelta(hours=8)).strftime("%Y-%m-%d %H:%M")

  '2011-01-20 07:03'

将字符串转换成时间对象
>>> endtime=datetime.datetime.strptime('20100701',"%Y%m%d")

>>> type(endtime)

  <type 'datetime.datetime'>

>>> print endtime

  2010-07-01 00:00:00

将从 1970-01-01 00:00:00 UTC 到现在的秒数,格式化输出  
>>> import time

>>> a = 1302153828

>>> time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(a))

  '2011-04-07 13:23:48'

8、命令行参数解析(getopt)

通常在编写一些日运维脚本时,需要根据不同的条件,输入不同的命令行选项来实现不同的功能
在Python中提供了getopt模块很好的实现了命令行参数的解析,下面距离说明。请看如下程序:

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import sys,os,getopt

def usage():

print '''''

Usage: analyse_stock.py [options...]

Options:

-e : Exchange Name

-c : User-Defined Category Name

-f : Read stock info from file and save to db

-d : delete from db by stock code

-n : stock name

-s : stock code

-h : this help info

test.py -s haha -n "HA Ha"

'''
try:

opts, args = getopt.getopt(sys.argv[1:],'he:c:f:d:n:s:')

except getopt.GetoptError:

usage()

sys.exit()

if len(opts) == 0:

usage()

sys.exit() 
for opt, arg in opts:

if opt in ('-h', '--help'):

  usage()

  sys.exit()

elif opt == '-d':

  print "del stock %s" % arg

elif opt == '-f':

  print "read file %s" % arg

elif opt == '-c':

  print "user-defined %s " % arg

elif opt == '-e':

  print "Exchange Name %s" % arg

elif opt == '-s':

  print "Stock code %s" % arg

elif opt == '-n':

  print "Stock name %s" % arg 
sys.exit()

9、print 格式化输出

9.1、格式化输出字符串
截取字符串输出,下面例子将只输出字符串的前3个字母

>>> str="abcdefg"

>>> print "%.3s" % str

  abc

按固定宽度输出,不足使用空格补全,下面例子输出宽度为10
>>> str="abcdefg"

>>> print "%10s" % str

     abcdefg

截取字符串,按照固定宽度输出
>>> str="abcdefg"

>>> print "%10.3s" % str

         abc

浮点类型数据位数保留
>>> import fpformat

>>> a= 0.0030000000005

>>> b=fpformat.fix(a,6)

>>> print b

  0.003000

对浮点数四舍五入,主要使用到round函数
>>> from decimal import *

>>> a ="2.26"

>>> b ="2.29"

>>> c = Decimal(a) - Decimal(b)

>>> print c

  -0.03

>>> c / Decimal(a) * 100

  Decimal('-1.327433628318584070796460177')

>>> Decimal(str(round(c / Decimal(a) * 100, 2)))

  Decimal('-1.33')

9.2、进制转换

有些时候需要作不同进制转换,可以参考下面的例子(%x 十六进制,%d 十进制,%o 十进制)

>>> num = 10

>>> print "Hex = %x,Dec = %d,Oct = %o" %(num,num,num)

  Hex = a,Dec = 10,Oct = 12

10、Python调用系统命令或者脚本

使用 os.system() 调用系统命令 , 程序中无法获得到输出和返回值

>>> import os

>>> os.system('ls -l /proc/cpuinfo')

>>> os.system("ls -l /proc/cpuinfo")

  -r--r--r-- 1 root root 0  3月 29 16:53 /proc/cpuinfo

  0

使用 os.popen() 调用系统命令, 程序中可以获得命令输出,但是不能得到执行的返回值
>>> out = os.popen("ls -l /proc/cpuinfo")

>>> print out.read()

  -r--r--r-- 1 root root 0  3月 29 16:59 /proc/cpuinfo 

使用 commands.getstatusoutput() 调用系统命令, 程序中可以获得命令输出和执行的返回值
>>> import commands

>>> commands.getstatusoutput('ls /bin/ls')

  (0, '/bin/ls')

11、Python 捕获用户 Ctrl+C ,Ctrl+D 事件

有些时候,需要在程序中捕获用户键盘事件,比如ctrl+c退出,这样可以更好的安全退出程序

try:

    do_some_func()

except KeyboardInterrupt:

    print "User Press Ctrl+C,Exit"

except EOFError:

    print "User Press Ctrl+D,Exit"

12、Python 读写文件

一次性读入文件到列表,速度较快,适用文件比较小的情况下

track_file = "track_stock.conf"

fd = open(track_file)

content_list = fd.readlines()

fd.close()

for line in content_list:

    print line 

逐行读入,速度较慢,适用没有足够内存读取整个文件(文件太大)
fd = open(file_path)

fd.seek(0)

title = fd.readline()

keyword = fd.readline()

uuid = fd.readline()

fd.close() 

写文件 write 与 writelines 的区别  

Fd.write(str) : 把str写到文件中,write()并不会在str后加上一个换行符
Fd.writelines(content) : 把content的内容全部写到文件中,原样写入,不会在每行后面加上任何东西

Python 相关文章推荐
python实现绘制树枝简单示例
Jul 24 Python
Python中使用bidict模块双向字典结构的奇技淫巧
Jul 12 Python
Python读取和处理文件后缀为.sqlite的数据文件(实例讲解)
Jun 27 Python
mac安装pytorch及系统的numpy更新方法
Jul 26 Python
pycharm中成功运行图片的配置教程
Oct 28 Python
python3+PyQt5 使用三种不同的简便项窗口部件显示数据的方法
Jun 17 Python
Django用户认证系统 Web请求中的认证解析
Aug 02 Python
numpy.random.shuffle打乱顺序函数的实现
Sep 10 Python
python读文件的步骤
Oct 08 Python
Python如何实现FTP功能
May 28 Python
Python是怎样处理json模块的
Jul 16 Python
python源码剖析之PyObject详解
May 18 Python
使用Python获取Linux系统的各种信息
Jul 10 #Python
Django中实现一个高性能计数器(Counter)实例
Jul 09 #Python
python实现的登录和操作开心网脚本分享
Jul 09 #Python
python实现的一个火车票转让信息采集器
Jul 09 #Python
python的描述符(descriptor)、装饰器(property)造成的一个无限递归问题分享
Jul 09 #Python
Python中__init__和__new__的区别详解
Jul 09 #Python
Python中使用logging模块代替print(logging简明指南)
Jul 09 #Python
You might like
1982年日本摄影师镜头下的中国孩子 那无忧无虑的童年
2020/03/12 杂记
PHP的宝库目录--PEAR
2006/10/09 PHP
php发送html格式文本邮件的方法
2015/06/10 PHP
php编程每天必学之验证码
2016/03/03 PHP
PHP设计模式之适配器模式定义与用法详解
2018/04/03 PHP
HTML代码中标签的全部属性 中文注释说明
2009/03/26 Javascript
node.js 一个简单的页面输出实现代码
2012/03/07 Javascript
js日期对象兼容性的处理方法
2014/01/28 Javascript
Javascript小技巧之生成html元素
2014/05/15 Javascript
让checkbox不选中即将选中的checkbox不选中
2014/07/11 Javascript
利用Javascript仿Excel的数据透视分析功能
2016/09/07 Javascript
Bootstrap按钮功能之查询按钮和重置按钮
2016/10/26 Javascript
jquery插件bootstrapValidator数据验证详解
2016/11/09 Javascript
jquery仿京东商品放大浏览页面
2017/06/06 jQuery
详解Angular2组件之间如何通信
2017/06/22 Javascript
Vue+element 解决浏览器自动填充记住的账号密码问题
2019/06/11 Javascript
原生js+css调节音量滑块
2020/01/15 Javascript
[07:49]2014DOTA2国际邀请赛 Newbee夺冠后采访xiao8坦言奖金会上交
2014/07/23 DOTA
[28:07]完美世界DOTA2联赛PWL S3 Phoenix vs INK ICE 第二场 12.13
2020/12/17 DOTA
python pandas 对时间序列文件处理的实例
2018/06/22 Python
基于numpy中数组元素的切片复制方法
2018/11/15 Python
使用Python刷淘宝喵币(低阶入门版)
2019/10/30 Python
pycharm第三方库安装失败的问题及解决经验分享
2020/05/09 Python
全球领先的美容用品专卖店:Beauty Plus Salon
2018/09/04 全球购物
Juicy Couture Beauty官方网站:香水和化妆品
2019/03/12 全球购物
一些Solaris面试题
2013/03/22 面试题
销售工作岗位职责
2013/12/24 职场文书
趣味游戏活动方案
2014/02/07 职场文书
药剂专业求职信
2014/06/20 职场文书
乡镇党的群众路线教育实践活动个人整改方案
2014/10/31 职场文书
大学生个人简历自我评价
2015/03/11 职场文书
2015年银行员工工作总结
2015/04/24 职场文书
2016年学校综治宣传月活动总结
2016/03/16 职场文书
python四个坐标点对图片区域最小外接矩形进行裁剪
2021/06/04 Python
python多线程方法详解
2022/01/18 Python
Spring Boot 使用 Spring-Retry 进行重试框架
2022/04/24 Java/Android