在 python 中遇到了一些困境。我想获取一个包含许多评论的 .txt 文件并将其拆分为一个列表。但是,我想拆分所有标点符号、空格和\n。当我运行以下 python 代码时,它会将我的文本文件分割成奇怪的地方。 注意:下面我只是尝试拆分时间段和结束时间来测试它。但它仍然经常去掉单词中的最后一个字母。
import regex as re
with open('G:/My Documents/AHRQUnstructuredComments2.txt','r') as infile:
nf = infile.read()
wList = re.split('. | \n, nf)
print(wList)
最佳答案
您需要修复引号并对正则表达式稍作更改:
import regex as re
with open('G:/My Documents/AHRQUnstructuredComments2.txt','r') as infile:
nf = infile.read()
wList = re.split('\W+' nf)
print(wList)
关于python - Python 中的 `re.split()` 工作异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45245081/