在大写字母处拆分字符串,但仅当Python中后跟小写字母时
我正在使用Python中的pdfminer.six来提取长文本数据。不幸的是,Miner并不总是工作得很好,尤其是在段落和文本包装方面。例如,我得到了以下输出:在大写字母处拆分字符串,但仅当Python中后跟小写字母时,python,split,text-mining,uppercase,Python,Split,Text Mining,Uppercase,我正在使用Python中的pdfminer.six来提取长文本数据。不幸的是,Miner并不总是工作得很好,尤其是在段落和文本包装方面。例如,我得到了以下输出: "2018Annual ReportInvesting for Growth and Market LeadershipOur CEO will provide you with all further details below." --> "2018 Annual Report Investin
"2018Annual ReportInvesting for Growth and Market LeadershipOur CEO will provide you with all further details below."
--> "2018 Annual Report Investing for Growth and Market Leadership Our CEO will provide you with all further details below."
现在,每当小写字母后面跟一个大写字母,然后是一个较小的字母(以及数字),我想插入一个空格。因此,“2018Annual”
最终变成了“2018 Annual”
,“ReportInvestment”
变成了“ReportInvestment”
,但“…CEO…”
仍然是“…CEO…”
我只找到了和的解决方案,但无法重写它。不幸的是,我是Python领域的新手。我们可以在这里尝试使用
re.sub
作为正则表达式方法:
inp=“2018Investment for Growth and Market Leadership年度报告我们的首席执行官将向您提供以下所有详细信息。”
inp=re.sub(r’(?尝试用正则表达式拆分:
import re
temp = re.sub(r"([A-Z][a-z]+)", r"\1", string).split()
string = ' '.join(temp)
我相信下面的代码给出了所需的结果
temp = re.sub(r"([a-z])([A-Z])", r"\1 \2", text)
temp = re.sub(r"(\d)([A-Za-z])", r"\1 \2", temp)
我仍然觉得复杂的正则表达式有点挑战性,因此需要将过程拆分为两个表达式。
也许在正则表达式方面有更好的人可以改进这一点,以展示如何以更优雅的方式实现它。即使是Python编码新手,您也应该尝试一些编码,并在询问解决方案之前发布您已经尝试过的内容。了解后,下次将尝试改进
import re
temp = re.sub(r"([A-Z][a-z]+)", r"\1", string).split()
string = ' '.join(temp)
temp = re.sub(r"([a-z])([A-Z])", r"\1 \2", text)
temp = re.sub(r"(\d)([A-Za-z])", r"\1 \2", temp)