Python: заменить несколько выражений регулярных выражений - PullRequest
0 голосов
/ 26 марта 2020

В следующем вводе я пытаюсь заменить числа и \n на '' и ' ' соответственно.

THE SONNETS\n\n                    1\n\nFrom fairest creatures we desire increase,\nThat thereby beauty’s rose might never die,\nBut as the riper should by time decease,\nHis

she hies,             1189\nAnd yokes her silver doves; by whose swift aid\nTheir mistress mounted through the empty skies,\nIn her light chariot quickly is convey’d;           1192\n  Holding their course to Paphos, where their queen\n  Means to immure herself and not be seen.\n'

input_var читается из файла с содержимым выше.

file_name = 'sample.txt'
file = open(folder+file_name, mode='r', encoding='utf8')
input_var = file.read()
file.close

Скриншот файла прилагается. enter image description here

Данные в файле

THE SONNETS

                    1

From fairest creatures we desire increase,
That thereby beauty’s rose might never die,
But as the riper should by time decease,
His

she hies,             1189
And yokes her silver doves; by whose swift aid
Their mistress mounted through the empty skies,
In her light chariot quickly is convey’d;           1192
  Holding their course to Paphos, where their queen
  Means to immure herself and not be seen.

Для идентификации номеров я использовал регулярное выражение [\s]{3,}\d{1,}\\n (должно быть не менее 3 пробелов перед номером. (см. эту ссылку для проверки регулярных выражений).

Я использую следующий код для замены регулярного выражения и \n оба, которые я получил из нескольких ответов в стеке потока.

Код 1 -

# Remove the numbers in sonnets and at the end of lines
pattern = {r'[\s]{3,}\d{1,}\\n' : '',
           r'\\n' : ' '
          }

regex = re.compile('|'.join(map(re.escape, pattern.keys(  ))))
output_var = regex.sub(lambda match: pattern[match.group(0)], input_var)

Код 2 -

rep = dict((re.escape(k), v) for k, v in pattern.items())
pattern_test = re.compile("|".join(rep.keys()))
output_var = pattern_test.sub(lambda m: rep[re.escape(m.group(0))], input_var)

Код 3 -

for i, j in pattern.items():
        output_var = input_var.replace(i, j)

, где input_var имеет вышеупомянутый текст. Все три ничего не заменяют.

Я также пытался

pattern = {r'[\s]{3,}\d{1,}\n' : '',
           r'\n' : ' '
          }

, но он ничего не заменяет.

pattern = {'[\s]{3,}\d{1,}\n' : '',
           '\n' : ' '
          }

заменяет только \n и вывод будет выглядеть как

THE SONNETS                      1  From fairest creatures we desire increase, That thereby beauty’s rose might never die, But as the riper should by time decease, His

Регулярное выражение не определено в словаре, и я думаю, что оно воспринимается как буквальная строка, а не как регулярное выражение. Как я могу указать регулярное выражение в словарь? Ответы, которые я нашел в stackoverflow, используют строки, а не регулярные выражения sion как ответ, предоставленный для этого вопроса .

Ожидаемый результат -

THE SONNETS                       From fairest creatures we desire increase, That thereby beauty’s rose might never die, But as the riper should by time decease, His

    she hies,And yokes her silver doves; by whose swift aid  Their mistress mounted through the empty skies, In her light chariot quickly is convey’d;  Holding their course to Paphos, where their queen   Means to immure herself and not be seen. ' 

Ответы [ 2 ]

0 голосов
/ 28 марта 2020

Вам необходимо запустить re.sub s в al oop, но убедитесь, что output_var инициализируется значением input_var:

output_var = input_var
for reg, repl in pattern.items():
  output_var = re.sub(reg, repl, output_var)

См. Демонстрационную версию Python онлайн :

import re

input_var = """THE SONNETS

                    1

From fairest creatures we desire increase,
That thereby beauty’s rose might never die,
But as the riper should by time decease,
His

she hies,             1189
And yokes her silver doves; by whose swift aid
Their mistress mounted through the empty skies,
In her light chariot quickly is convey’d;           1192
  Holding their course to Paphos, where their queen
  Means to immure herself and not be seen."""

pattern = {r'\s{3,}\d+\n' : '',
           r'\n' : ' '}
output_var = input_var
for reg, repl in pattern.items():
  output_var = re.sub(reg, repl, output_var)

print(output_var)

Вывод:

THE SONNETS From fairest creatures we desire increase, That thereby beauty’s rose might never die, But as the riper should by time decease, His  she hies,And yokes her silver doves; by whose swift aid Their mistress mounted through the empty skies, In her light chariot quickly is convey’d;  Holding their course to Paphos, where their queen   Means to immure herself and not be seen.
0 голосов
/ 26 марта 2020

Вот небольшой рабочий пример, который вы можете запустить (если у вас есть bs4 et c.). Я вижу, что вы получаете помощь по нумерации и регулярным выражениям, но это может помочь понять, что строка возвращает et c. (не совсем уверен, что цель). Не удалось найти источник в Интернете с таким же номером, как у вашего источника, поэтому, к сожалению, он не похож на другой. Может быть, пища для размышлений, если ничего другого.

from bs4 import BeautifulSoup
import re
import requests


url = 'http://www.gutenberg.org/cache/epub/1041/pg1041.txt'

page = requests.get(url)
# print(page.status_code)
soup = BeautifulSoup(page.text)

sonnet = page.text

print(sonnet[780:1500])
print()
print('------')
print()
sonnet = re.sub('\r','',sonnet)
sonnet = re.sub('\n','',sonnet)
print(sonnet[698:1500])

url2 = 'http://shakespeare.mit.edu/Poetry/VenusAndAdonis.html'

page = requests.get(url2)
# print(page.status_code)
soup = BeautifulSoup(page.text)
print()
print('------')
print('------')
print()
VenusAndAdonis = soup.text
print(type(VenusAndAdonis))
print(VenusAndAdonis[800:1500])
print()
print('------')
print()
VenusAndAdonis = re.sub('\r','',VenusAndAdonis)
VenusAndAdonis = re.sub('\n',' ',VenusAndAdonis)
print(VenusAndAdonis[800:1500])

Выходы:

I

  From fairest creatures we desire increase,
  That thereby beauty's rose might never die,
  But as the riper should by time decease,
  His tender heir might bear his memory:
  But thou, contracted to thine own bright eyes,
  Feed'st thy light's flame with self-substantial fuel,
  Making a famine where abundance lies,
  Thy self thy foe, to thy sweet self too cruel:
  Thou that art now the world's fresh ornament,
  And only herald to the gaudy spring,
  Within thine own bud buriest thy content,
  And tender churl mak'st waste in niggarding:
    Pity the world, or else this glutton be,
    To eat the world's due, by the grave and thee.

  II

  When forty winters shall besiege thy brow,

------

I  From fairest creatures we desire increase,  That thereby beauty's rose might never die,  But as the riper should by time decease,  His tender heir might bear his memory:  But thou, contracted to thine own bright eyes,  Feed'st thy light's flame with self-substantial fuel,  Making a famine where abundance lies,  Thy self thy foe, to thy sweet self too cruel:  Thou that art now the world's fresh ornament,  And only herald to the gaudy spring,  Within thine own bud buriest thy content,  And tender churl mak'st waste in niggarding:    Pity the world, or else this glutton be,    To eat the world's due, by the grave and thee.  II  When forty winters shall besiege thy brow,  And dig deep trenches in thy beauty's field,  Thy youth's proud livery so gazed on now,  Will be a tatter'd weed of small 

------
------

<class 'str'>
 honour to your heart's content; which I
wish may always answer your own wish and the world's hopeful
expectation.
Your honour's in all duty,
WILLIAM SHAKESPEARE.

EVEN as the sun with purple-colour'd face
Had ta'en his last leave of the weeping morn,
Rose-cheek'd Adonis hied him to the chase;
Hunting he loved, but love he laugh'd to scorn;
Sick-thoughted Venus makes amain unto him,
And like a bold-faced suitor 'gins to woo him.


'Thrice-fairer than myself,' thus she began,
'The field's chief flower, sweet above compare,
Stain to all nymphs, more lovely than a man,
More white and red than doves or roses are;
Nature that made thee, with herself at strife,
Saith that the world hath ending wit

------

 honour to your heart's content; which I wish may always answer your own wish and the world's hopeful expectation. Your honour's in all duty, WILLIAM SHAKESPEARE.  EVEN as the sun with purple-colour'd face Had ta'en his last leave of the weeping morn, Rose-cheek'd Adonis hied him to the chase; Hunting he loved, but love he laugh'd to scorn; Sick-thoughted Venus makes amain unto him, And like a bold-faced suitor 'gins to woo him.   'Thrice-fairer than myself,' thus she began, 'The field's chief flower, sweet above compare, Stain to all nymphs, more lovely than a man, More white and red than doves or roses are; Nature that made thee, with herself at strife, Saith that the world hath ending wit
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...