下载链接:http://cdn1.python3.vip/files/py/prac_re.zip 个人版的参考答案:
"""
@Project :python
@Author : 文跃锐(yuerwen)
@Time : 2021/09/25
@File :yuerwen_附加练习.py
"""
'''
下载zip包,解压后出现一个 prac_re 目录,该目录中有很多文件。
请写代码,检查目录中所有文件,找出包含如下格式的文本
https://www.bilibili.com/video/av74106411/?p=60
将数字的值改为 +3, 比如,上面的链接就需要改为
https://www.bilibili.com/video/av74106411/?p=63
如果链接后面的是 p=1 就要改为 p=4, 是 p=99 就要改为 p=102,
最后将修改结果写回文件。
'''
import time
import os
path = 'prac_re'
def getCopy(number):
newStr = ''
for (dirpath,dirname,filesname) in os.walk(path):
for filename in filesname:
fpath = os.path.join(dirpath,filename)
with open(fpath,'r',encoding='utf8') as f:
flist = f.read().splitlines()
for f in flist:
if 'https://www.bilibili.com/video/av74106411/?p=' in f:
location = f.find('https://www.bilibili.com/video/av74106411/?p=')
locationStar = location + len('https://www.bilibili.com/video/av74106411/?p=')
locationEnd = locationStar
while f[locationEnd].isdigit():
locationEnd += 1
if int(f[locationStar:locationEnd]) == 1:
numberRet = int(f[locationStar:locationEnd].replace(int(f[locationStar:locationEnd]),4))
elif int(f[locationStar:locationEnd]) == 99:
numberRet = int(f[locationStar:locationEnd].replace(int(f[locationStar:locationEnd]),102))
else:
numberRet = int(f[locationStar:locationEnd]) + number
str2 = f[:locationStar] + str(numberRet) + f[locationEnd:]
newStr += str2
with open(fpath, 'w', encoding='utf') as f1:
f1.write(newStr)
getCopy(3)
|