s=list(open('c:\\sample\\customer.txt'))
Ls=[]
for x in s:
Ls.append(x.split())
import time
sy,sm,sd=time.strftime('%Y-%m-%d',time.localtime(time.time())).split('-')
sy=int(sy)
sm=int(sm)
sd=int(sd)
Ld=[]
for x in Ls:
a=x[1]
by=int(a[6:10])
bm=int(a[10:12])
bd=int(a[12:14])
age=sy-by
if sm<bm:
age-=1
elif sm==bm and sd<bd:
age-=1
if int(a[16])%2==0:
sex='女'
else:
sex='男'
Ld.append([x[0],sex,age])
print(Ld)
f=open('c:\\sample\\cusinfo.txt','w')
for x in Ld:
f.write('%s\t%s\t%d\n'%(x[0],x[1],x[2]))
f.close()
s=list(open('c:\\sample\\customer.txt'))
Ls=[]
for x in s:
Ls.append(x.split())
import time
sy,sm,sd=time.strftime('%Y-%m-%d',time.localtime(time.time())).split('-')
sy=int(sy)
sm=int(sm)
sd=int(sd)
Ld=[]
for x in Ls:
a=x[1]
by=int(a[6:10])
bm=int(a[10:12])
bd=int(a[12:14])
age=sy-by
if sm<bm:
age-=1
elif sm==bm and sd<bd:
age-=1
if int(a[16])%2==0:
sex='女'
else:
sex='男'
Ld.append([x[0],sex,age])
print(Ld)
f=open('c:\\sample\\cusinfo.txt','w')
for x in Ld:
f.write('%s\t%s\t%d\n'%(x[0],x[1],x[2]))
f.close()
f = open('D:\\Python\\各章实验内容答案与素材参考\\ch5\\文本文件\\customer.txt','r')n = open('D:\\name.txt','w')name = ''sex = ''age = ''whole = ''wri = ''import timewhile True: line = f.readline() if line == '': break else: plist = line.split() name = plist[0] if int(plist[1][-2])%2==0: sex = '女' else: sex = '男'######all good###########left age... by = int(plist[1][6:9+1]) bm = int(plist[1][10:11+1]) bd = int(plist[1][12:13+1]) sy,sm,sd=time.strftime('%Y-%m-%d',time.localtime(time.time())).split('-') sy=int(sy) sm=int(sm) sd=int(sd) if bd<=sd and bm<=sm: age = str(sy-by) elif bd>sd: if bm<sm: age = str(sy-by) elif bm>=sm: age = str(sy-by-1) whole = name+'\t'+sex+'\t'+age+'\n' wri += wholen.write(wri)f.close()n.close()
冗长。。。