我正在尝试对m3u文件进行更改。我唯一想要实现的就是将“tvg name=”中的内容替换为“tvg id=”
以下是m3u的示例:
#EXTINF:0 user-agent="Firefox/100.0" tvg-id="" tvg-name="TR I beIN MOVIES PREMIER HD 1080P (beIN)" tvg-logo="" group-title="--SINEMA KANALLARI--",TR I beIN MOVIES PREMIER HD 1080P (beIN)
http://xxxxxxx
#EXTINF:0 user-agent="Firefox/100.0" tvg-id="" tvg-name="TR I beIN MOVIES PREMIER 2 1080P (beIN)" tvg-logo="" group-title="--SINEMA KANALLARI--",TR I beIN MOVIES PREMIER 2 1080P (beIN)
http://xxxxxxx
#EXTINF:0 user-agent="Firefox/100.0" tvg-id="" tvg-name="TR I beIN MOVIES ACTION 1080P (beIN)" tvg-logo="" group-title="--SINEMA KANALLARI--",TR I beIN MOVIES ACTION 1080P (beIN)
http://xxxxxxx
我不擅长正则表达式(也不擅长python),但我的代码如下:
import re
line = open('z.m3u', "r", encoding='utf-8')
read_lines=line.read()
pattern=re.compile(r'tvg-id=(".*?").tvg-name=(".*?")')
matches=pattern.finditer(read_lines)
for match in matches:
tvg_id=match.group(1)
tvg_name=match.group(2)
w=re.sub(tvg_id,tvg_name,read_lines)
print(w)
line.write(w)
但是,我无法使用更改写入文件。如果您能帮忙,我们将不胜感激
谢谢
替换不需要正则表达式,因为替换的是精确的字符串,而不是基于用于捕获的正则表达式
改变
w=re.sub(tvg_id,tvg_name,read_lines)
为了
w = read_lines.replace('tvg-name=' + tvg_name, 'tvg-name=' + tvg_id)
它可能会起作用
相关问题 更多 >
编程相关推荐