diff --git a/py/酒店源.py b/py/酒店源.py index 5642bda..cc76c73 100644 --- a/py/酒店源.py +++ b/py/酒店源.py @@ -515,6 +515,35 @@ with open("iptv.txt", 'a', encoding='utf-8') as file: file.write(f"{channel_name},{channel_url}\n") print("频道列表文件iptv.txt追加写入成功!") +def remove_duplicates(input_file, output_file): + # 用于存储已经遇到的URL和包含genre的行 + seen_urls = set() + seen_lines_with_genre = set() + # 用于存储最终输出的行 + output_lines = [] + # 打开输入文件并读取所有行 + with open(input_file, 'r', encoding='utf-8') as f: + lines = f.readlines() + print("去重前的行数:", len(lines)) + # 遍历每一行 + for line in lines: + # 使用正则表达式查找URL和包含genre的行,默认最后一行 + urls = re.findall(r'[https]?[http]?[P2p]?[mitv]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', line) + genre_line = re.search(r'\bgenre\b', line, re.IGNORECASE) is not None + # 如果找到URL并且该URL尚未被记录 + if urls and urls[0] not in seen_urls: + seen_urls.add(urls[0]) + output_lines.append(line) + # 如果找到包含genre的行,无论是否已被记录,都写入新文件 + if genre_line: + output_lines.append(line) + # 将结果写入输出文件 + with open(output_file, 'w', encoding='utf-8') as f: + f.writelines(output_lines) + print("去重后的行数:", len(output_lines)) + +# 使用方法 +remove_duplicates('iptv.txt', 'iptv.txt') ######################################################################################################################