从身份号码中计算年龄

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()


冗长。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值