Python 文本处理 论语
2021/5/15 1:25:12
本文主要是介绍Python 文本处理 论语,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1.问题描述:请编写程序,提取《论语》文档中所有原文内容,输出保存到“论语-提取版.txt”文件。输出文件格式要求:去掉文章中原文部分每行行首空格及如“1.11”等的数字标志,行尾无空格、无空行。参考格式如下(原文中括号及内部数字是对应源文件中注释项的标记):
1 j=7 2 k=0 3 a=0 4 b=0 5 l=[] 6 content=[] 7 8 try: 9 with open(r'C:\Users\DELL\Desktop\论语.txt','r',encoding='utf-8') as file1: 10 for line in file1: 11 newline=line 12 if newline[2:5] in [str(m)+'·'+str(n) for m in range(1,25) for n in range(1,25)]\ 13 or newline[2:6] in [str(m)+'·'+str(n) for m in range(1,25) for n in range(1,25)]\ 14 or newline[2:7] in [str(m)+'·'+str(n) for m in range(1,25) for n in range(1,25)]\ 15 or newline[2:8] in [str(m)+'·'+str(n) for m in range(1,25) for n in range(1,25)]: 16 for p in [str(m)+'·'+str(n) for m in range(45,0,-1) for n in range(45,0,-1)]: 17 if p in newline[0:9]: 18 newline2=newline.replace(p,'') 19 content.append(newline2) 20 break 21 22 else: 23 content.append(newline) 24 25 26 with open(r'C:\Users\DELL\Desktop\论语改2.txt','w',encoding='utf-8') as file2: 27 for i in range(len(content)): 28 29 if '【原文】' in content[i] and i>=b: 30 31 a=i 32 k=i 33 34 while k!=0: 35 if '】' in content[k+1]: 36 b=k+1 37 l.append([a,b]) 38 break 39 else: 40 k+=1 41 for m,n in l: 42 for line in content[m+1:n-1]: 43 file2.write(line) 44 45 46 47 48 49 except Exception as t: 50 print(t)
这篇关于Python 文本处理 论语的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-27使用python 将ETH账户的资产打散
- 2024-09-26Python编程基础
- 2024-09-2610 种方法写出更好的 Python 代码
- 2024-09-25Python编程基础详解
- 2024-09-25Python编程入门教程
- 2024-09-25从零开始使用Python构建LLaMA 3
- 2024-09-23Python中理解和使用树形结构的简单教程
- 2024-09-23Python 编程基础入门
- 2024-09-18初探Python股票自动化交易:入门指南
- 2024-09-18Python量化入门:轻松掌握量化分析基础与实战