栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python技巧分享(十四)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python技巧分享(十四)


这是一个系列文章,主要分享python的使用建议和技巧,每次分享3点,希望你能有所收获。

1 排列组合

示例程序:

#!/usr/bin/env python# coding=utf8import itertoolsfor p in itertools.permutations('ABC', 2):    print p'''
('A', 'B')
('A', 'C')
('B', 'A')
('B', 'C')
('C', 'A')
('C', 'B')
'''for c in itertools.combinations('ABC', 2):    print c'''
('A', 'B')
('A', 'C')
('B', 'C')
'''

通过itertools模块,可以很方便实现元素的排列和组合。由示例中可以看到,分别从ABC三个字母中取2个字母,实现其排列和组合,itertools模块还有很多有用功能,感兴趣可以看看。

2 创建临时文件

示例程序:

#!/usr/bin/env python# -*- coding: utf-8 -*-import tempfile


TEMP_FILE = tempfile.NamedTemporaryFile()print 'temp file name: <{self.name}>n'.format(self=TEMP_FILE)with open(TEMP_FILE.name, 'w') as f:
    f.write("line 1nline 2nline 3n")with open(TEMP_FILE.name) as f:    for line in f.readlines():        print line

运行示例:

$ python tmp_file_demo.py
temp file name: 

line 1

line 2

line 3

$ ls /tmp/tmpVSppeA
ls: cannot access /tmp/tmpVSppeA: No such file or directory

借助tempfile模块,可以很方便的操作临时文件。由示例中可以看到,创建的临时文件/tmp/tmpVSppeA在使用完毕后会自动删除,不需要手动删除该文件,tempfile模块还有很多有用功能,感兴趣可以看看。

3 打印信息到标准错误

示例程序:

#!/usr/bin/env python# coding=utf8from __future__ import print_functionimport sysdef eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)


eprint("print to stderr")
print("print to stdout")'''
print to stderr
print to stdout
'''

运行示例:

$ python print_stderr.pyprint to stderrprint to stdout
$ python print_stderr.py > /tmp/stdout.logprint to stderr
$ python print_stderr.py 2> /tmp/stderr.logprint to stdout
$ python print_stderr.py > /tmp/stdout_and_stderr.log 2>&1
$ cat /tmp/stdout.logprint to stdout
$ cat /tmp/stderr.logprint to stderr
$ cat /tmp/stdout_and_stderr.logprint to stderrprint to stdout

通过导入__future__模块的print_function,将print函数改造成python3的print,就可以实现将输出打印到标准错误。由示例中可以看到,通过封装一个新的函数eprint,实现类似print的打印功能,唯一区别就是eprint函数将输出打印到标准错误,而不是标准输出。



作者:songleo
链接:https://www.jianshu.com/p/93e773da7456


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/222646.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号