2

我对正则表达式和匹配文本文件有一个问题,我是 python 新手。我的文件看起来像:

epg_slo3.txt:10346224:        Service_ID: 1 (0x0001)  [=  --> refers to PMT program_number]
epg_slo3.txt:10346236:            Start_time: 0xdce0112500 [= 2013-09-09 11:25:00 (UTC)]
epg_slo3.txt:10346237:            Duration: 0x0001000 [=  00:10:00 (UTC)]
epg_slo3.txt:10346246:                  event_name: "..©port"  -- Charset: ISO/IEC 8859  special table

我需要做的是,我需要这样的东西:

Service_ID: 1 (0x0001)  [=  --> refers to PMT program_number]: --> Program 1
Start_time: 0xdce0112500 [= 2013-09-09 11:25:00 (UTC)]: --> Start 2013-09-09 11:25:00 (UTC)
Duration: 0x0001000 [=  00:10:00 (UTC)] --> Duration 00:10:00 (UTC)
event_name: "..©port"  -- Charset: ISO/IEC 8859  --> Category ©port

我的代码如下所示:

#!/usr/bin/python
import codecs
import re

BLOCKSIZE = 1048576

with codecs.open('epg_slo10.txt', "r", "iso-8859-2") as sourceFile:
    with codecs.open('epg_slo.txt', "w", "utf-8") as targetFile:
        while True:
            contents = sourceFile.read(BLOCKSIZE)
            if not contents:
                break
            targetFile.write(contents)


input_file  = open('epg_slo.txt', "r")
output_file = open('epg_slo_kategorije.txt', "w")

for line in input_file:
    line = line.replace("Service_ID:","Program")
    line = line.replace("Start_time:","Start")
    line = line.replace("event_name:","Title")
    output_file.write(line)

你能帮我解决这个问题吗

谢谢阅读。BR!

4

2 回答 2

1

line = line.replace您的代码之前,添加以下行:

line = re.sub(r'^epg_slo3.txt:\d{8}:\s*','', line)

例如。
如果

line = "epg_slo3.txt:10346224:        Service_ID: 1 (0x0001)  [=  --> refers to PMT program_number]"

然后在调用之后re.sub

line = "Service_ID: 1 (0x0001)  [=  --> refers to PMT program_number]"
于 2013-09-11T07:46:03.337 回答
1

regex用空字符串“”替换下面给出的

/^epg_slo3.txt:\d{8}:\s*/
于 2013-09-11T07:13:54.063 回答