Питонический способ вставить запятую перед заглавными буквами [Regex] [duplicate]

1

У моего Regex-fu нет недостатка, и я не могу обойти его... любая помощь, полученная очень сильно.

Я ищу способ Python для синтаксического анализа строки, из которой вырывается устаревшая старая часть программного обеспечения (из которой я не имею доступа к источнику):

,Areas for further improvement,The schools leaders are rightly seeking to improve the following areas:,,========2========,,3/5,Continue to focus on increasing performance at the higher levelsPupils,literacy and numeracy skills across the curriculumStandards,in science throughout the schoolPupils,numerical reasoning skills

Я хочу сделать следующее:

(1) Удалите все существующие символы ,: =/ чтобы сформировать единую непрерывную строку:

Areas for further improvementThe schools leaders are rightly seeking to improve the following areas23/5Continue to focus on increasing performance at the higher levelsPupilsliteracy and numeracy skills across the curriculumStandardsin science throughout the schoolPupilsnumerical reasoning skills

Затем предидуйте каждую заглавную букву одним , чтобы позволить мне использовать строку в качестве разумного входа csv....

,Areas for further improvement,The schools leaders are rightly seeking to improve the following areas23/5,Continue to focus on increasing performance at the higher levels,Pupilsliteracy and numeracy skills across the curriculum,Standardsin science throughout the school,Pupilsnumerical reasoning skills

Я ценю, что это даст мне преимущество, но я могу это исключить, когда пишу в файл.

Возможно ли это с помощью re.sub() и regex-fu?

(Счастлив, что это будет двухэтапный процесс - удалите существующие мусорные символы, а затем добавьте их, заглавными заглавными буквами)

Может кто-нибудь сохранить мое здравое мышление?

ура

Теги:

2 ответа

3
re.sub(r'([A-Z])', r',\1', re.sub(r'[,:=/]', '', input_))

выход:

',Areas for further improvement,The schools leaders are rightly seeking to improve the following areas235,Continue to focus on increasing performance at the higher levels,Pupilsliteracy and numeracy skills across the curriculum,Standardsin science throughout the school,Pupilsnumerical reasoning skills'
  • 0
    Поработал, и я поняла вложенный re.sub - люди просто знают материал для регулярных выражений или есть где-то в Интернете секретный соус для создания выражений?
  • 0
    Вы можете практиковать здесь . Вложенная часть не слишком сложна. Это как b = re.sub(e1, r1, a); c = re.sub(b, e2, r2, b) .
Показать ещё 1 комментарий
1

Вы можете применить re.sub дважды:

import re
s = ',Areas for further improvement,The schools leaders are rightly seeking to improve the following areas:,,========2========,,3/5,Continue to focus on increasing performance at the higher levelsPupils,literacy and numeracy skills across the curriculumStandards,in science throughout the schoolPupils,numerical reasoning skills'
new_s = re.sub('[A-Z]', lambda x:f',{x.group()}', re.sub('[,:\=]+', '', s))

Выход:

',Areas for further improvement,The schools leaders are rightly seeking to improve the following areas23/5,Continue to focus on increasing performance at the higher levels,Pupilsliteracy and numeracy skills across the curriculum,Standardsin science throughout the school,Pupilsnumerical reasoning skills'

Ещё вопросы

Сообщество Overcoder
Наверх
Меню