首页 > 编程语言 >python 排版模块

python 排版模块

时间:2022-08-16 15:24:09浏览次数:52  
标签:elif return python text self 排版 模块 isnumeric True



# 科技类文本
# 用阿拉伯数字连续编号,不同层次的数字之间加下圆点相隔(即圆点加在数字的右下角),最后数字后面不加标点,如“1”“1.1”“1.1.1”……

# 人文类文本
# 第一层用“一、”,第二层用“(一)”,第三层用“1.”,第四层用“(1)”,第五层用 “ ① ”

class Typesetting():
def __init__(self,text):
self.text = ''.join(text)
def division(self,i,t):
if i < len(self.text)-1:
# A. a.
if t.encode('utf-8').isalpha() == True and self.text[i+1] == '.' :
return True
# A、 a、
elif t.encode('utf-8').isalpha() == True and self.text[i+1] == '、' :
return True

# 1、 一、
elif self.text[i - 1].isnumeric() == False and t.isnumeric() == True and self.text[i + 1] == '、':
return True
# 11、 一十、
elif t.isnumeric() == True and self.text[i+1].isnumeric() == True and self.text[i+2] == '、' :
return True

# 1. 一.
elif self.text[i - 1] != '.' and self.text[i - 1].isnumeric() == False and t.isnumeric() == True and \
self.text[i + 1] == '.' and self.text[i + 2].isnumeric() == False:
return True
# 11. 一十.
elif self.text[i-1] != '.' and t.isnumeric() == True and self.text[i+1].isnumeric() == True and self.text[i+2] == '.' :
return True

# 1.1.1.1
elif t.isnumeric() == True and \
self.text[i + 1] == '.' and \
self.text[i + 2].isnumeric() == True and \
self.text[i + 3] == '.' and \
self.text[i + 4].isnumeric() == True and \
self.text[i + 5] == '.' and \
self.text[i + 6].isnumeric() == True:
return True
# 11.1.1.1
elif t.isnumeric() == True and \
self.text[i + 1].isnumeric() == True and \
self.text[i + 2] == '.' and \
self.text[i + 3].isnumeric() == True and \
self.text[i + 4] == '.' and \
self.text[i + 5].isnumeric() == True and \
self.text[i + 6] == '.' and \
self.text[i + 7].isnumeric() == True:
return True
# 11.11.1.1
elif t.isnumeric() == True and \
self.text[i + 1].isnumeric() == True and \
self.text[i + 2] == '.' and \
self.text[i + 3].isnumeric() == True and \
self.text[i + 4].isnumeric() == True and \
self.text[i + 5] == '.' and \
self.text[i + 6].isnumeric() == True and \
self.text[i + 7] == '.' and \
self.text[i + 8].isnumeric() == True:
return True
# 11.11.11.1
elif t.isnumeric() == True and \
self.text[i + 1].isnumeric() == True and \
self.text[i + 2] == '.' and \
self.text[i + 3].isnumeric() == True and \
self.text[i + 4].isnumeric() == True and \
self.text[i + 5] == '.' and \
self.text[i + 6].isnumeric() == True and \
self.text[i + 7].isnumeric() == True and \
self.text[i + 8] == '.' and \
self.text[i + 9].isnumeric() == True:
return True

# 1.1.1
elif self.text[i-1] != '.' and t.isnumeric() == True and self.text[i+1] == '.' and self.text[i+2].isnumeric() == True and self.text[i+3] == '.' and self.text[i+4].isnumeric() == True :
return True
# 11.1.1
elif self.text[i - 1] != '.' and \
t.isnumeric() == True and \
self.text[i + 1].isnumeric() == True and \
self.text[i + 2] == '.' and \
self.text[i + 3].isnumeric() == True and \
self.text[i + 4] == '.' and \
self.text[i + 5].isnumeric() == True:
return True
# 11.11.1
elif self.text[i - 1] != '.' and \
t.isnumeric() == True and \
self.text[i + 1].isnumeric() == True and \
self.text[i + 2] == '.' and \
self.text[i + 3].isnumeric() == True and \
self.text[i + 4].isnumeric() == True and \
self.text[i + 5] == '.' and \
self.text[i + 6].isnumeric() == True:
return True

# 1.1
elif (self.text[i-1] == '。' or self.text[i-1] == ',') and self.text[i-1] != '.' and t.isnumeric() == True and self.text[i+1] == '.' and self.text[i+2].isnumeric() == True :
return True
# 11.1
elif (self.text[i - 1] == '。' or self.text[i - 1] == ',') and self.text[i - 1] != '.' and t.isnumeric() == True and self.text[i + 2].isnumeric() == True and self.text[i + 3] == '.' and self.text[i + 4].isnumeric() == True:
return True

# (1) (一)
elif t == '(' and self.text[i+1].isnumeric() == True and self.text[i+2] == ')' :
return True
# (11) (一十)
elif t == '(' and self.text[i + 1].isnumeric() == True and self.text[i + 2].isnumeric() and self.text[i + 3] == ')':
return True


# ① 暂未解决 无法用isnumeric()函数 虽然对此符号可识别但是也对其他数字识别所以会造成混乱
elif t in ['①','②','③','④','⑤','⑥','⑦','⑧','⑨','⑩',
'⑴','⑵','⑶','⑷','⑸','⑹','⑺','⑻','⑼','⑽','⑾','⑿','⒀','⒁','⒂','⒃','⒄','⒅','⒆','⒇',
'⒈', '⒉', '⒊', '⒋', '⒌', '⒍', '⒎', '⒏', '⒐', '⒑', '⒒', '⒓', '⒔', '⒕', '⒖', '⒗', '⒘', '⒙', '⒚', '⒛',
] :
return True

# 第1章 第一章
elif t == '第' and self.text[i+1].isnumeric() == True and self.text[i+2] == '章' :
return True
# 第11章 第一十章
elif t == '第' and self.text[i + 1].isnumeric() == True and self.text[i + 2].isnumeric() == True and self.text[i + 3] == '章':
return True

# 第1节 第一节
elif t == '第' and self.text[i+1].isnumeric() == True and self.text[i+2] == '节' :
return True
# 第11节 第一十节
elif t == '第' and self.text[i + 1].isnumeric() == True and self.text[i + 2].isnumeric() == True and self.text[i + 3] == '节':
return True


# 1)二)
elif self.text[i-1] != '(' and t.isnumeric() == True and self.text[i+1] == ')':
return True
# 11)二十)
elif self.text[i - 1] != '(' and t.isnumeric() == True and self.text[i + 1].isnumeric() == True and self.text[i + 2] == ')':
return True

# 1)、 一)、
elif t.isnumeric() == True and self.text[i+1] == ')' and self.text[i+2] == '、':
return True
# 11)、 一十)、
elif t.isnumeric() == True and self.text[i + 1] == ')' and self.text[i + 2].isnumeric() == True and self.text[i + 3] == '、':
return True


def start(self):
str = ''
for i,t in enumerate(self.text):
if self.division(i,t) == True :

str = '%s\n%s' % (str,t)
# print(str)
else:
str = '%s%s' % (str, t)
return str
text = open('text.txt','r',encoding='utf-8').read()
#调用编译模块
a = Typesetting(text).start()
print(a)
# text = open('text.txt','w',encoding='utf-8')
# text.write(a)

标签:elif,return,python,text,self,排版,模块,isnumeric,True
From: https://www.cnblogs.com/lld76/p/16470717.html

相关文章