我在一个文本文件中有一些超链接.我想将第一行上的链接与下一个相邻行进行比较,并按编号创建链接. 例如,
请考虑以下相邻链接
https://gp.to/ab/394/las69-02-09-2020/
https://gp.to/ab/394/las69-02-09-2020/4/
这里的输出文件将是:
https://gp.to/ab/394/las69-02-09-2020/
https://gp.to/ab/394/las69-02-09-2020/2/
https://gp.to/ab/394/las69-02-09-2020/3/
https://gp.to/ab/394/las69-02-09-2020/4/
同样地,我也需要为其他线路做……
示例输入:
https://gp.to/ab/394/las69-02-09-2020/
https://gp.to/ab/394/las69-02-09-2020/4/
https://gp.to/ab/563/dimp-02-07-2023/
https://gp.to/ab/39443/omegs-02-07-2023/
https://gp.to/ab/39443/omegs-02-07-2023/3/
https://gp.to/ab/39443/lis-22-04-2018/
https://gp.to/ab/39443/lis-22-04-2018/2/
https://gp.to/ab/39443/madi-22-04-2018/
https://gp.to/ab/39443/madi-22-04-2018/5/
输出示例:
https://gp.to/ab/394/las69-02-09-2020/
https://gp.to/ab/394/las69-02-09-2020/2/
https://gp.to/ab/394/las69-02-09-2020/3/
https://gp.to/ab/394/las69-02-09-2020/4/
https://gp.to/ab/563/dimp-02-07-2023/
https://gp.to/ab/39443/omegs-02-07-2023/
https://gp.to/ab/39443/omegs-02-07-2023/2/
https://gp.to/ab/39443/omegs-02-07-2023/3/
https://gp.to/ab/39443/lis-22-04-2018/
https://gp.to/ab/39443/lis-22-04-2018/2/
https://gp.to/ab/39443/madi-22-04-2018/
https://gp.to/ab/39443/madi-22-04-2018/2/
https://gp.to/ab/39443/madi-22-04-2018/3/
https://gp.to/ab/39443/madi-22-04-2018/4/
https://gp.to/ab/39443/madi-22-04-2018/5/
我试过...
# Function to extract the number from a URL
def extract_number(url):
parts = url.split('/')
for part in parts[::-1]:
if part.isdigit():
return int(part)
return None
# Read the input file
with open('input.txt', 'r') as input_file:
lines = input_file.readlines()
output_lines = []
# Iterate through the input lines and generate output lines
for i in range(len(lines)):
current_url = lines[i].strip()
output_lines.append(current_url)
if i + 1 < len(lines):
next_url = lines[i + 1].strip()
current_number = extract_number(current_url)
next_number = extract_number(next_url)
if current_number is not None and next_number is not None:
for num in range(current_number + 1, next_number):
new_url = current_url.rsplit('/', 1)[0] + '/' + str(num) + '/'
output_lines.append(new_url)
# Write the output to a file
with open('output.txt', 'w') as output_file:
output_file.writelines(output_lines)
但我没有得到想要的输出.