以下python代码主要用于向一个文件输出一组数据信息
#!/python
from random import randint, choice
from string import lowercase
from sys import maxint
from time import ctime
import time
import os
doms = ('com', 'edu', 'net', 'org', 'gov')
#file create
fname = time.strftime('%Y-%m-%d', time.localtime(time.time())) + '_lch'
#if not os.path.exists(fname):
fobj = open(fname,'w')
for i in range(randint(5,10)):
dtint = randint(0,maxint-1)
dtstr = ctime(dtint)
showter = randint(4,7)
em = ''
for j in range(showter):
em += choice(lowercase)
longer = randint(showter, 12)
dn = ''
for j in range(longer):
dn += choice(lowercase)
temp = '%s::%s@%s.%s::%d-%d-%d' % (dtstr, em, dn, choice(doms), dtint, showter, longer)
print temp
fobj.write('%s\n' % temp)
fobj.close()
文件名 2010-12-01_lch
输出信息如下(因为是随机的,可能不一样):
Mon Mar 26 05:36:19 1984::[email protected]::449098579-7-7
Thu Sep 11 01:34:17 2008::[email protected]::1221068057-5-9
Tue May 04 03:54:38 1993::[email protected]::736458878-4-6
Sat Oct 27 04:48:53 2029::[email protected]::1887742133-4-10
Wed Mar 22 02:43:10 1972::[email protected]::70051390-6-6
Fri Mar 08 13:23:53 2030::[email protected]::1899177833-4-5
Sat Sep 25 23:27:34 2032::[email protected]::1979738854-5-10
Fri Jan 11 10:10:08 2002::[email protected]::1010715008-7-8
Thu Jan 04 13:37:52 2024::[email protected]::1704346672-7-12
Tue Jan 03 17:12:38 2006::[email protected]::1136279558-4-11
接下来我们通过这个程序来提取2010-12-01_lch文件的邮件地址信息
import re
fobj = open('2010-12-01_lch','r')
patt = '\w+@\w+\.(com|edu|net|org|gov)'
for eachLine in fobj.readlines():
print re.search(patt, eachLine).group()
fobj.close()
提取的邮件地址信息如下:
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
下面这段代码练习异常:
#!python
#
# myexc.py
#
import os, socket, errno, types, tempfile
from tempfile import mktemp
class NetworkError(IOError):
pass
class FileError(IOError):
pass
def upArgs(args, newarg=None):
if isinstance(args, IOError):
myargs = []
myargs.extend([arg for arg in args])
else:
myargs = list(args)
if newarg:
myargs.append(newarg)
return tuple(myargs)
def fileArgs(file, mode, args):
if args[0] == errno.EACCES and 'access' in dir(os):
perms = ''
permd = {'r':os.R_OK, 'w':os.W_OK, 'x':os.X_OK}
pkeys = permd.keys()
pkeys.sort()
pkeys.reverse()
for eachPerm in 'rwx':
if os.access(file, permd[eachPerm]):
perms += eachPerm
else:
perms +='-'
if isinstance(args, IOError):
myargs = []
myargs.extend([arg for arg in args])
else:
myargs = list(args)
myargs[1] = "'%s' %s (perms: '%s')" % (mode, myargs[1], perms)
myargs.append(args.filename)
else:
myargs = args
return tuple(myargs)
def myconnect(sock, host, port):
try:
sock.connect((host, port))
except socket.error, args:
myargs = upArgs(args)
if len(myargs) == 1:
myargs = (errno.ENXIO, myargs[0])
raise NetworkError, upArgs(myargs, host +':'+str(port))
def myopen(file, mode='r'):
try:
fo = open(file, mode)
except IOError, args:
raise FileError, fileArgs(file, mode, args)
return fo
def testfile():
file = mktemp()
f = open(file, 'w')
f.close()
for eachTest in ((0, 'r'), (0100, 'r'),
(0400, 'w'), (0500, 'w')):
try:
os.chmod(file, eachTest[0])
f = myopen(file, eachTest[1])
except FileError, args:
print "%s: %s" % \
(args.__class__.__name__, args)
else:
print file, "opened ok... perm ignored"
f.close()
os.chmod(file, 0777) # enable all perms
os.unlink(file)
def testnet():
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
for eachHost in ('deli', 'www'):
try:
myconnect(s,'192.1.9.125',80)
except NetworkError, args:
print "%s: %s" % (args.__class__.__name__, args)
if __name__ == '__main__':
testnet()
testfile()
下面这一段主要是我在写这些小的练习时的一些随笔(都写在了一个文件里面了)
#import string
#for x in range(1, 11):
# print string.rjust('x', 2)
#import os
#fobj = open('text.txt','w')
#fobj.writelines('test')
#fobj.close()
#import time
#print time.time()
#print time.localtime(time.time())
#print time.strftime('%Y-%m-%d', time.localtime(time.time()))
import re
patt = '\w+@\w+\.(com|gov|edu|net|org)'
temp = 'Mon Mar 26 05:36:19 1984::[email protected]::449098579-7-7'
print re.search(patt,temp).group()