读取文本最后一行: f = open('test11.txt', 'rb') for i in f: offset = -16 while True: f.seek(offset, 2) data = f.readlines() if len(data) > 1: print("文件的最后一行是:%s"%(data[-1].decode('gbk'))) break offset *= 2 优点: 使用for i in f是使用一行读取一行,不会消耗太多的内存,如果使用readl
python每次处理一个字符的三种方法 a_string = "abccdea" print 'the first' for c in a_string: print ord(c)+1 print "the second" result = [ord(c)+1 for c in a_string] print result print "the thrid" def do_something(c): return ord(c)+1 result
用Python写了一个postgresql函数,感觉很爽 CREATE LANGUAGE plpythonu; postgresql函数 CREATE OR REPLACE FUNCTION myfun1(text) RETURNS text AS $BODY$ s = args[0] h = 0; n = len(s); for i, c in enumerate(s): h = h + ord(c)*31**(n-1-i); bits = 4*8; return (h +