python - Python计算拆分句子的单词?
问题描述
不确定如何删除输出末尾的“\n”
基本上,我有这个 txt 文件,其中包含以下句子:
"What does Bessie say I have done?" I asked.
"Jane, I don't like cavillers or questioners; besides, there is something truly forbidding in a child
taking up her elders in that manner.
Be seated somewhere; and until you can speak pleasantly, remain silent."
我设法用分号用代码分割句子:
import re
with open("testing.txt") as file:
read_file = file.readlines()
for i, word in enumerate(read_file):
low = word.lower()
re.split(';',low)
但不确定如何将拆分句子的单词数为 len() 不起作用:句子的输出:
['"what does bessie say i have done?" i asked.\n']
['"jane, i don\'t like cavillers or questioners', ' besides, there is something truly forbidding in a
child taking up her elders in that manner.\n']
['be seated somewhere', ' and until you can speak pleasantly, remain silent."\n']
例如第三句话,我想数左边的 3 个单词和右边的 8 个单词。
谢谢阅读!
解决方案
`
import re
sentences = [] #empty list for storing result
with open('testtext.txt') as fileObj:
lines = [line.strip() for line in fileObj if line.strip()] #makin list of lines allready striped from '\n's
for line in lines:
sentences += re.split(';', line) #spliting lines by ';' and store result in sentences
for sentence in sentences:
print(sentence +' ' + str(len(sentence.split()))) #out
推荐阅读
- python - 根据熊猫中的类别标准化值
- c++ - 如何在没有函数的情况下编写这段代码并在 C++ 中变得更简单?
- python - 无法从目录启动 Gunicorn:13 - 权限被拒绝
- python - 如何将文件路径放入ansible中的变量中?
- javascript - Node/JS - 类构造函数变量未定义
- reactjs - 是否可以将 Next.js 与自定义服务器和静态/自动优化一起使用?
- python - 在数据框的子集中查找单词
- android - 尝试使用 uiautomatorviewer 时无法检测 adb 版本,没有退出值
- flowtype - 使类型成为流中对象的值
- javascript - 如何从 express.js 运行服务器与客户端共享桌面屏幕?