iptv_sort.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import re
  2. # 读取iptv.txt文件,提取频道信息
  3. channels = []
  4. with open('IPTV.txt', 'r', encoding='utf-8') as file:
  5. for line in file:
  6. line = line.strip()
  7. if line:
  8. channel, address = line.split(',')
  9. channels.append((channel, address))
  10. # 对频道进行排序
  11. channels.sort()
  12. # 自定义排序函数,提取频道名称中的数字并按数字排序
  13. def channel_key(channel):
  14. match = re.search(r'\d+', channel)
  15. if match:
  16. return int(match.group())
  17. else:
  18. return float('inf') # 返回一个无穷大的数字作为关键字
  19. # 对频道进行排序
  20. channels.sort(key=lambda x: channel_key(x[0]))
  21. # 生成iptv_list.txt文件
  22. with open('iptv_sort.txt', 'w', encoding='utf-8') as file:
  23. file.write('央视频道,#genre#\n')
  24. for channel, address in channels:
  25. if 'cctv' in channel.lower():
  26. file.write(f'{channel},{address}\n')
  27. file.write('卫视频道,#genre#\n')
  28. for channel, address in channels:
  29. if '卫视' in channel:
  30. file.write(f'{channel},{address}\n')
  31. file.write('其他频道,#genre#\n')
  32. for channel, address in channels:
  33. if 'cctv' not in channel.lower() and '卫视' not in channel:
  34. file.write(f'{channel},{address}\n')