我正在尝试将一个巨大的 .text 文件拆分为多个 .text 文件,每个文件仅包含一个段落。
让我举个例子。我需要这样的文字:
这是第一段。这没有任何意义,因为这只是一个例子。
这是第二段,和前一段一样毫无意义。
另存为两个独立的 .txt 文件,其中包含第一段(第一个文件)和第二段(第二个文件)。
第一个文件只有:“这是第一段。它没有意义,因为这只是一个例子。”
第二段:“这是第二段,和前一段一样毫无意义。”
整个文本也是如此。在巨大的 .txt 文件中,段落被一个或多个空行分隔。有想法吗?
非常感谢!
我创建了一个 3 段示例并使用您的评论here https://stackoverflow.com/questions/39926993/split-text-file-into-paragraph-files-in-r#comment67149431_39926993重现我认为你所描述的内容。
text <- "This is the first paragraph. It makes no sense because is just an example. Nothing makes sense and I'm trying to understand what I'm doing with life. This paragraph does not seem to end.
What are we doing here.
This a second paragraph, as meaningless as the previous one.
There's too much to do - this is meaningless though.
Wow, that's funny."
paras <- unlist(strsplit(text, "\n\n"))
for (i in 1:length(paras)) {
write.table(paras[i], file = paste0("paragraph", i, ".txt"), row.names = F)
}
此代码首先将值分配给变量 text,然后使用带有参数“\n\n”的 strsplit 函数在每个双换行符处拆分文本。
然后,使用 for 循环遍历每个元素并将其保存到单独的 .txt 文件中。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)