Python Regex sub не заменяет разрывы строк

#python #regex #line-breaks

#python #регулярное выражение #разрывы строк

Вопрос:

У меня есть робот, который приносит мне html-код, подобный этому:

 <div class="std">
  <p>CAR:
    <span>Onix</span>
  </p>
  <p>MODEL: LTZ</p>
  <p>
    <span>COLOR:
    <span>Black</span>
  </p>
  <p>ACESSORIES:
    <span>ABS</span>
  </p>
  <p>
    <span>DESCRIPTION:</span>
    <span>The Chevrolet Onix is a subcompact car launched by American automaker Chevrolet in Brazil at the 2012 São Paulo International Motor Show[1] to succeed some versions of Chevrolet Celta. Offered initially as a five-door hatchback, a four-door sedan was launched in 2013 and called the Chevrolet Prisma.[2] The Onix is currently only sold in some South American countries part of Mercosur, including Brazil, Argentina, Colombia, Paraguay and Uruguay.</span>
  </p>
  <p>TECHNICAL DETAIL:
    <span>The Onix is available in three trim levels (LS, LT and LTZ) with two 4-cylinder engines, the 1.0-litre producing 78 PS (57 kW; 77 bhp) (petrol)/ 80 PS (59 kW; 79 bhp) (ethanol) and 1.4-litre 98 PS (72 kW; 97 bhp) (petrol)/106 PS (78 kW; 105 bhp) (ethanol) offering automatic or five-speed manual transmission..</span>
  </p>
</div>
 

Я применил приведенный ниже код для удаления тегов HTML:

 cleanr    = re.compile('<.*?>')
cleantext = re.sub(cleanr,'n', html_code).strip()
 

Он возвращается ко мне:

 CAR: Onix


MODEL: LTZ


COLOR:
Black



ACESSORIES:
ABS



DESCRIPTION:


The Chevrolet Onix is a subcompact car launched by American automaker Chevrolet in Brazil at the 2012 São Paulo International Motor Show[1] to succeed some versions of Chevrolet Celta. Offered initially as a five-door hatchback, a four-door sedan was launched in 2013 and called the Chevrolet Prisma.[2] The Onix is currently only sold in some South American countries part of Mercosur, including Brazil, Argentina, Colombia, Paraguay and Uruguay.



TECHNICAL DETAIL:
The Onix is available in three trim levels (LS, LT and LTZ) with two 4-cylinder engines, the 1.0-litre producing 78 PS (57 kW; 77 bhp) (petrol)/ 80 PS (59 kW; 79 bhp) (ethanol) and 1.4-litre 98 PS (72 kW; 97 bhp) (petrol)/106 PS (78 kW; 105 bhp) (ethanol) offering automatic or five-speed manual transmission..
 

Теперь мне нужно удалить разрывы строк, чтобы получить что-то вроде этого:

 CAR: Onix
MODEL: LTZ
COLOR: Black
ACESSORIES: ABS
DESCRIPTION: The Chevrolet Onix is a subcompact car launched by American automaker Chevrolet in Brazil at the 2012 São Paulo International Motor Show[1] to succeed some versions of Chevrolet Celta. Offered initially as a five-door hatchback, a four-door sedan was launched in 2013 and called the Chevrolet Prisma.[2] The Onix is currently only sold in some South American countries part of Mercosur, including Brazil, Argentina, Colombia, Paraguay and Uruguay.
TECHNICAL DETAIL: The Onix is available in three trim levels (LS, LT and LTZ) with two 4-cylinder engines, the 1.0-litre producing 78 PS (57 kW; 77 bhp) (petrol)/ 80 PS (59 kW; 79 bhp) (ethanol) and 1.4-litre 98 PS (72 kW; 97 bhp) (petrol)/106 PS (78 kW; 105 bhp) (ethanol) offering automatic or five-speed manual transmission..
 

Я попробовал этот код ниже, но он неправильно соответствует разрывам строк:

 cleantext = re.sub(r':s*[rn]*', ': ', cleantext)
 

Я также попробовал этот другой код:

 cleantext = cleantext.replace(': n', ': ')
 

Это также не работает. Как я могу с этим справиться?

Комментарии:

1. Что вы пытаетесь сделать ? вы хотите записать выходные данные во внешний файл или просто распечатать на консоли?

2. На самом деле я пытаюсь перехватить данные после двоеточия. Поскольку эти данные поступают без шаблона, я хочу сначала все упорядочить, а затем получить данные.

3. В первую очередь всегда следует избегать использования регулярных выражений для синтаксического анализа .

Ответ №1:

Я думаю, что ваша проблема состоит из двух частей, первая — объединить строку в две строки, как показано ниже
COLOR:
Black

Для
COLOR: black

а затем удалите все пустые строки

Для первой части вы могли бы использовать replace your re.sub следующим образом
cleantext = re.sub(r'(.*):s*[rn](.*)', 'g<1>: g<2>', cleantext)

И для удаления пустых строк было бы сложно сделать это через re.sub, поэтому я бы предложил использовать cleantext = "n".join([line for line in cleantext.split('n') if line.strip() != ''])

Это даст вам ответ, как и ожидалось

Ответ №2:

Я думаю, это должно сработать для вас

 >>> string = """
CAR: Onix


MODEL: LTZ


COLOR:
Black



ACESSORIES:
ABS



DESCRIPTION:


The Chevrolet Onix is a subcompact car launched by American automaker Chevrolet in Brazil at the 2012 São Paulo International Motor Show[1] to succeed some versions of Chevrolet Celta. Offered initially as a five-door hatchback, a four-door sedan was launched in 2013 and called the Chevrolet Prisma.[2] The Onix is currently only sold in some South American countries part of Mercosur, including Brazil, Argentina, Colombia, Paraguay and Uruguay.



TECHNICAL DETAIL:
The Onix is available in three trim levels (LS, LT and LTZ) with two 4-cylinder engines, the 1.0-litre producing 78 PS (57 kW; 77 bhp) (petrol)/ 80 PS (59 kW; 79 bhp) (ethanol) and 1.4-litre 98 PS (72 kW; 97 bhp) (petrol)/106 PS (78 kW; 105 bhp) (ethanol) offering automatic or five-speed manual transmission..
"""
>>> list_string = string.split("nnn")
>>> for each in list_string:
    print each.replace("n","").strip()


CAR: Onix
MODEL: LTZ
COLOR:Black
ACESSORIES:ABS
DESCRIPTION:
The Chevrolet Onix is a subcompact car launched by American automaker Chevrolet in Brazil at the 2012 São Paulo International Motor Show[1] to succeed some versions of Chevrolet Celta. Offered initially as a five-door hatchback, a four-door sedan was launched in 2013 and called the Chevrolet Prisma.[2] The Onix is currently only sold in some South American countries part of Mercosur, including Brazil, Argentina, Colombia, Paraguay and Uruguay.
TECHNICAL DETAIL:The Onix is available in three trim levels (LS, LT and LTZ) with two 4-cylinder engines, the 1.0-litre producing 78 PS (57 kW; 77 bhp) (petrol)/ 80 PS (59 kW; 79 bhp) (ethanol) and 1.4-litre 98 PS (72 kW; 97 bhp) (petrol)/106 PS (78 kW; 105 bhp) (ethanol) offering automatic or five-speed manual transmission..