python - 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."
解决方案
str.split()
使用固定分隔符而不是正则表达式拆分。你需要使用re.split()
.
import re
sep = re.split(r'Document [a-zA-Z0-9]{25}\n', content)
推荐阅读
- ios - 在表格视图单元格中时,如何根据其内容快速设置 CollectionView 高度?
- javascript - React Native 状态不更新
- bash - 在 bash 中将兆比特转换为千比特
- google-chrome - 单击页面中的任意位置时,Chrome 将我送回 Elements 选项卡
- tensorflow - 如何使用使用自定义操作的模型从源代码构建张量流,这些操作是现有操作的重命名版本?
- r - 基于 R 中的列值(或日期)的滚动回归
- javascript - 是否有允许在后台窗口中使用 RequestAnimationFrame 的浏览器(用于测试目的,macOS)
- oracle - 将记录类型作为参数传递有什么问题
- c# - Doxygen 不适用于 C# 中的二维数组
- ios - iOS 二进制未加密测试飞行构建