【发布时间】:2020-09-11 16:48:53
【问题描述】:
不确定如何删除输出末尾的“\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个字。
感谢阅读!
【问题讨论】:
-
你不能只用空白分割并得到结果列表的长度吗?
-
这能回答你的问题吗? Count Words in Python
-
结帐
.splitlines() -
正则表达式也有像 \b 和 \w 这样的东西,它们可能会对你有所帮助。您应该举例说明您的目标是作为此类数据的结果。
标签: python python-3.x nlp