首页 > 解决方案 > split() 没有产生预期的结果

问题描述

我对 python split 有一个问题,我无法弄清楚我缺少什么导致 split 函数无法正常工作。我以前一直在使用类似的拆分,它们工作得很好。

content=open(file).read)()
Sep = content.split(r'Document [a-zA-Z0-9]{25}\n')

我正在阅读的文件非常简单:

"I like coffee.

Document CLASSAR020181030eeat0000l

I like tea as well.

Document CLASSAR020181030eeat0000l

I like both coffee and tea."

标签: pythonsplit

解决方案


str.split()使用固定分隔符而不是正则表达式拆分。你需要使用re.split().

import re
sep = re.split(r'Document [a-zA-Z0-9]{25}\n', content)

推荐阅读