|#!/bin/env python
# -*- coding: utf-8 -*-
# Объединение абзацев разде-
# разделенных переносами
# 19.10.21
import sys, os
#--------------------------------------
def EndStr(s):
, ,SQ = ''
, ,for a in reversed(s):
, , , ,if a == ' ':
, , , , , ,return SQ[:-1]
, , , ,else:
, , , , , ,SQ = a+SQ
#--------------------------------------
old_List = [] # Сюда читается
new_List = [] # Промеждуточный список
#--------------------------------------
#FN = input('Введите имя файла:')
#fn1=path+FN
FN = '1.txt'
fb2_file=open(FN,'r')
#fb2_file=open(FN,'r', encoding='utf-8')
old_List=fb2_file.readlines()
fb2_file.close()
#--------------------------------------
SS = '' # Промежуточное хранение строки
SQ = '' # Начало слова "разор-
"разорванного" "странным" переносом
FlagQ = False
i = 0
Первый проход. Поиск "странных" переносов
for item in old_List:
, ,s = item.strip()# Обрезание пробелов
, ,if s == '':
, , , ,new_List.append(s)
, , , ,i += 1
, , , ,if i > len(old_List):
, , , , , ,new_List.append(item[i+1])
, , , , , ,break
, , , ,continue
, ,
, ,if (s[-1] == '-'): # последний символ строки
, , , ,SQ = EndStr(s) # следующая строка начинается с SQ
, , , ,if (SQ != None) and (SQ != '') and (old_List[i+1].find(SQ)==0):
, , , , , ,m = -(len(SQ)+1)
, , , , , ,new_List.append(s[:m]+'@') # помечаем строку для последующего объединения
, , , ,else:
, , , , , ,new_List.append(s)
, ,else:
, , , ,new_List.append(s)
, ,i += 1
, ,if i > len(old_List):
, , , ,new_List.append(item[i+1])
, , , ,break
old_List.clear()
# второй проход. соединение строк
#for item in reversed(new_List):
for item in new_List:
, ,if item == '':
, , , ,if SS != '':
, , , , , ,old_List.append(SS)
, , , , , ,SS = ''
, , , ,old_List.append(item)
, , , ,continue
# , ,print(item)
, ,if FlagQ:
, , , ,SS = SS[:-1] + item
, , , ,FlagQ = item[-1] == '@'
, ,else:
, , , ,if SS != '':
, , , , , ,old_List.append(SS)
, , , , , ,SS = ''
, , , ,if item[-1] == '@': # последний символ строки
, , , , , ,FlagQ = True
, , , , , ,SS = item
, , , ,else:
, , , , , ,old_List.append(item)
, , , , , , , ,
if SS != '':# запись последней строки
, ,old_List.append(SS)
#--------------------------------------
fn2="2_.txt"
new_file=open(fn2,'w')
for i in old_List:
, , new_file.write(i+'\n')
new_file.close()
print('Done')