Форум сайта python.su
2
Здраствуйте, уважаемые форумчане. Недавно пытался написать простой текст, который бы вырезал из файла номера страниц и колонтикулы (очевидно, остались от сканирования), но возникли проблемы. Долго думал, как организовать работу (я новичек) и получилось нечто эдакое - через списки:
# -*- coding: utf-8 -*- # Список исключений list_ex = [] for num_ex in range(13, 20): list_ex.append(repr(num_ex) + "") print list_ex # Файловый список input_file = open('read1.txt', 'r') list_text = [] for line in input_file: list_text.append(line.rstrip()) print list_text # Очистка от номера страницы list_clear = [] for words in list_text: if words not in list_ex != True: list_clear.append(words) print list_clear # Вывод в файл output_file = open('clear_text.txt', 'w') for clear_words in list_clear: output_file.write(clear_words.strip() + '\n')
list_clear = [] for words in list_text: if words not in list_ex[0] != True: list_clear.append(words) list_ex.pop(0) else: num_text = list_text.count(words) num_ex = list_ex.count(num_text) + 1 list_ex.pop(:num_ex)
Офлайн
25
import re
is_numeric_re = re.compile('\d+')
with open('text_to_clear.txt') as fd:
lines = map(str.strip, fd)
dest = []
skip_next = False
total = len(lines) - 1
for idx, ln in enumerate(lines):
if skip_next:
skip_next = False
continue
if ln and is_numeric_re.match(ln) is None:
dest.append(ln)
elif (idx < total) and lines[idx + 1]:
dest.append(ln)
else:
skip_next = True
print '\n'.join(dest)
Офлайн
857
s0rgвсе отступы будут удаленыlines = map(str.strip, fd)
Razorудалить или сжать до одного переноса ?
2) Как удалить двойной Enter в тексте методами Питона?
Razorесть re.sub(), за два прохода можно сделать и то, и другое
Были пару размышлений:
1) Пропарсить страницу побуквенно.
Офлайн
25
py.user.nextТочно )
все отступы будут удалены
Офлайн