首页新闻找找看学习计划

python re 练习

0
[已关闭问题] 关闭于 2016-01-14 17:18

Vamei前辈出的练习题,个人解答,留存记忆。

http://www.cnblogs.com/vamei/archive/2012/08/31/2661870.html

 

 

 

import os,re
import datetime


filename = "output_1981.10.21.txt"
print "Old file name is: " + filename

gettime = re.search("(?P<year>\d{4}).(?P<month>\d{2}).(?P<day>\d{2})",filename)
year = gettime.group("year")
month = gettime.group("month")
day = gettime.group("day")

date = year + "-" + month + "-" + day
print "Get the date is: " + date


transform = datetime.date(int(year),int(month),int(day))
weekday = transform.weekday()+1
print date + " is week: " + str(weekday)

fix_name = "output_" + year + "-" + month + "-" + day + "-" + str(weekday) + ".txt"

if os.path.exists(filename):
    os.rename(filename,fix_name)
    print "rename complete"
    print "New file name is: " + fix_name
else:
    print "This file is not exist here: " + filename
west_Tang风的主页 west_Tang风 | 菜鸟二级 | 园豆:201
提问于:2016-01-14 17:17
< >
分享
所有回答(1)
0

re.sub 还需要加强理解和运用。

west_Tang风 | 园豆:201 (菜鸟二级) | 2016-01-14 17:18
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册