-5

大家好,我对正则表达式有疑问,我需要一些帮助。我的代码如下所示:

# -*- coding: utf-8 -*-
import re

WEEKDAYS = ["nedjelja", "utorak", "četvrtak", "ponedjeljak", "subota", "srijeda", "petak"]:

with open('natio_geo_channel.xml', 'r') as input_file, \
        open('nat.xml', 'w') as output_file:
    for line in input_file:
        for x in WEEKDAYS
            line = line.replace("<para>" + x, "<date>")
        line = re.sub(r"<para>\d{0}", "<start>", line)
        line = re.sub(r"<start>\d{2}\.\d{2}\s/\s/", "</start>", line)
        output_file.write(line)

我的文件看起来像:

<para>nedjelja1. rujna 2013.</para>
    <para>06.00        na hrvatskom Zona gradnje: Izgradnja zelenog Pekinga</para>
    <para>Kineske nevolje sa zagađenjem problem su s globalnim posljedicama. Pratite ekipu zelenih inženjera koji grade energetski učinkoviti Peking.</para>

我所做的,我首先替换为并在其中删除 nedjelja 没关系。但我该怎么做:

<start>06:00<start><title>Zona gradnje</title><sub>Izgradnja zelenog Pekinga</sub>

由此<para>06.00 na hrvatskom Zona gradnje: Izgradnja zelenog Pekinga</para>

能给我一些建议或思路吗?

4

2 回答 2

1

请试试这个:

def main():    
    line = r'<para>06.00        na hrvatskom Zona gradnje: Izgradnja zelenog Pekinga</para>'        
    if re.search(r'^(<para>)\d{2}.\d{2}', line):
        line_time = re.findall(r'\d{2}.\d{2}',line)[0]       
        line_title = line[line.find(re.findall(r'[A-Z]',line)[0]):line.find(':')]        
        line_sub = line[line.find(':')+1:line.find(r'</')].strip()

        print '<start>'+line_time+'</start><title>'+line_title+'</title><sub>'+line_sub+'</sub>'   

让我知道这是否是您需要的。

输出

<start>06.00</start><title>Zona gradnje</title><sub>Izgradnja zelenog Pekinga</sub>

使用它从文件中读取行:

 with open(r'D:\Trading\PythonScholar\input\input.tx','r') as file: 
        for line in file:            
            if re.search(r'^(<para>)\d{2}.\d{2}', line.strip()): 
                line_time = re.findall(r'\d{2}.\d{2}',line)[0] 
                line_title = line[line.find(re.findall(r'[A-Z]',line)[0]):line.find(':')] 
                line_sub = line[line.find(':')+1:line.find(r'</')].strip() 
                print '<start>'+line_time+'</start><title>'+line_title+'</title><sub>'+line_sub+'</sub>'

希望这可以帮助。

于 2013-10-01T14:26:27.060 回答
1

要转换这个:

<para>06.00        na hrvatskom Zona gradnje: Izgradnja zelenog Pekinga</para>

对此:

<start>06:00<start><title>Zona gradnje</title><sub>Izgradnja zelenog Pekinga</sub>

做这个:

str = re.sub(".*?>(\S+)(?:\s+\S+){2}\s+(.*?):\s*(.*)<.*", 
    "<start>\1<start><title>\2</title><sub>\3</sub>", str)
于 2013-10-01T21:36:09.853 回答