python - 如何在 .txt 文件中的一组下一行单词中的一个点之后下一行
问题描述
我的代码有问题。我有一个文本文件,这个文本文件里面有一千个来自一个句子的标签/下一行单词。我的问题是我想恢复这个文本文件中的单词并再次使它成为一个句子。
我想到了一种方法,它是一个 for 循环语句,如果它命中了点.
,那么它将把句子存储在列表中。
with('test','r') as f:
text = f.open()
sentence = []
sentences = []
for words in text:
if words != "."
sentence.append(words)
elif words == "."
sentence.append(words)
sentences.append(sentence)
sentence = []
#Sample output
#[['This', 'is', 'a', 'sentence', '.'], ['This', 'is', 'the', 'second', 'sentence', '.'],
#['This', 'is', 'the', 'third', 'sentence', '.']],
#This is the text file
This
is
a
sentence
.
This
is
the
second
sentence
.
This
is
thr
third
sentence
.
代码有点工作,但有点复杂。我发现了一个更短且不那么复杂的想法。先感谢您。
解决方案
以下是同一班轮,如果需要更多帮助,请告诉我:
sentences = open('test','r').read().split('\.')
推荐阅读
- python - 气流只创建一次重复
- javascript - 单击刷新然后返回按钮时如何登陆到现有页面
- r - 如何使用缺失值对R中面板数据集的异常值进行winsorize
- pyspark - 使用 spark-xml 解析 XML(不工作)
- c# - 从函数 [C#]、[ASP.NET Core] 返回多个对象
- bash - 将shell脚本中的变量传递给内联ruby命令?
- token - 智能合约执行费用的决定因素
- python - Python Dataframe 计算元素列表的最小值和最大值
- jquery - 试图从另一个函数访问数组
- javascript - 如何将 txt 文件中的数据转换为 Node 中的对象数组