python常用工具大全

文件操作

读文件

按行读取文件

1
2
3
4
f = open(file_name, 'r')
for line in f:
// do something with line
f.close()

读取文件最后一行

1
2
f = open(file_name, 'r')
last_line = f.readlines()[-1]

list 操作

把list形状的字符串转化为list

  • 方法1

    1
    2
    3
    import ast
    line_str = '[12569, 399470, 387182]'
    line_list = ast.literal_eval(line_str)
  • 方法2

    1
    2
    line_str = '[12569, 399470, 387182]'
    line_list = line_str[1:len(line_str)-1].split(",")
注意,这里有个坑,就是ast.literal_eval耗时超级大,如果能够很简单的转换list,则不要用ast.literal_eval,推荐使用方法2

两个list数组相加

  • 方法1

    1
    2
    3
    4
    5
    6
    7
    import numpy
    first = [1, 2, 3, 4, 5]
    second = [6, 7, 8, 9, 10]
    a = numpy.array(first)
    b = numpy.array(second)
    c= a+b
    print c
  • 方法2

    1
    2
    3
    4
    first = [1, 2, 3, 4, 5]
    second = [6, 7, 8, 9, 10]
    result = [x + y for x, y in zip(first, second)]
    print result

lists中list相加

1
2
3
lists_of_lists = [[1, 2, 3], [4, 5, 6]]
result = [sum(x) for x in zip(*lists_of_lists)]
print result

数字类型相关操作

float

  1. float 保留两位小数
    1
    2
    f_num = 3.1415926
    round(f_num, 2)

格式化输出

1
2
msg = "{}\t{}\t{}".format("hello", "world", "!")
print(msg)

python main函数接收参数

1
2
3
4
5
6
7
8
9
10
11
12
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("-o", "--object",
help="the cmd to master or master's http services", metavar="master/press")
parser.add_argument("-c", "--command",
help="the command to execute", metavar="start/stop")
parser.add_argument("-i", "--idc",
help="the idc want to start, default is [all]", metavar="[nj/njjs/hba/tc/hna/hnb]")

args = parser.parse_args()

print vars(args) # vars(args)可以使参数转化为字典

ssh 相关

ssh 远程执行命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import subprocess
import sys

HOST="www.example.org"
# Ports are handled in ~/.ssh/config since we use OpenSSH
COMMAND="uname -a"

ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND],
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
result = ssh.stdout.readlines()
if result == []:
error = ssh.stderr.readlines()
print >>sys.stderr, "ERROR: %s" % error
else:
print result

python 执行shell命令

1
2
3
4
5
6
import subprocess
# 用subprocess库获取返回值。
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
out, err = p.communicate()
for line in out.splitlines():
print (line)

python开启http ftp服务

可以用来文件传输,方法一

1
python -m SimpleHTTPServer

方法二

1
2
jumbo install python-httpfileserver
httpfileserver

profile函数和程序耗时

  1. 采样,运行:

    1
    python -m cProfile -o pprof.out main.py argv1 #如果如参数,则可以不用输入argv1
  2. 分析,按照时间排序

    1
    python -c "import pstats; p=pstats.Stats('pprof.out'); p.sort_stats('time').print_stats()" | less

中括号[]的正则匹配

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import re
def test_regex():
pattern = re.compile(r'.*\[([^\[\]]*)\].*uniq=((\d+))')
line = 'what is [most import] this is not what i want, uniq=1235235 and others'
m = pattern.match(line)
if not m or len(m.groups()) != 3:
print ("error")
else:
print (m.group(0))
print (m.group(1))
print (m.group(2))

if __name__ == '__main__':
test_regex()

python中文乱码

1
2
3
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

python md5

1
2
3
4
5
6
7
8
9
10
from hashlib import md5
def test_md5():
str = "hello world, 世界你好"
res1 = md5(str).digest() # 8进制,即byte字节
res2 = md5(str).hexdigest() # 16进制
print (res1)
print (res2)

if __name__ == '__main__':
test_md5()

python输出结果
shell输出结果