在一个文档中定位到第一个空行,读取其后的所有文本
get.msg <- function(path)
{
con <- file(path, open = "rt", encoding = "latin1")
text <- readLines(con)
# The message always begins after the first full line break
msg <- text[seq(which(text == "")[1]+1, length(text), 1)]
close(con)
return(paste(msg, collapse = "\n"))
}
报错:
Error in seq.default(which(text == "")[1]+1, length(text), 1) :
'from' cannot be NA, NaN or infinite
示例文档:
From [email protected] Thu Aug 22 13:17:22 2002
Return-Path: <[email protected]>
Delivered-To: [email protected]
Received: from localhost (localhost [127.0.0.1])
by phobos.labs.spamassassin.taint.org (Postfix) with ESMTP id 136B943C32
for <zzzz@localhost>; Thu, 22 Aug 2002 08:17:21 -0400 (EDT)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Dwindows-1252" http-equiv=3DContent-T=
ype>
在一个文档中定位到第一个空行,读取其后的所有文本 get.msg <- function(path) { con <- file(path, open = "rt", encoding = "latin1") text <- readLines(con) # The message always begins after the first full line break msg <-... 文档定位第空行读取其所文本
get.msg <- function(path)
{
con <- file(path, open = "rt", encoding = "latin1")
text <- readLines(con)
# The message always begins after the first full line break
msg <- text[seq(which(text == "")[1]+1, length(text), 1)]
close(con)
return(paste(msg, collapse = "\n"))
}
报错:
Error in seq.default(which(text == "")[1]+1, length(text), 1) :
'from' cannot be NA, NaN or infinite 展开 示例文档:
From [email protected] Thu Aug 22 13:17:22 2002
Return-Path: <[email protected]>
Delivered-To: [email protected].org
Received: from localhost (localhost [127.0.0.1])
by phobos.labs.spamassassin.taint.org (Postfix) with ESMTP id 136B943C32
for <zzzz@localhost>; Thu, 22 Aug 2002 08:17:21 -0400 (EDT)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Dwindows-1252" http-equiv=3DContent-T=
ype>
本回答被网友采纳我知道seq函数的用法。我只是想问,这里为什么会报错
追答你的from是空的,你需要设定一下from,我觉得你用which(text == "")[1]+1作为from不对吧,你这样写不懂什么意思
追问需求是从那段文本中定位第一个空行,那么text==“”,该写什么呢?无论我写什么,都会报错。把==改成!=就不报错,但是结果还是不对
追答但是你的文本里有 多个空格, 这样表述感觉不正确
追问[1]不是表示第一个么?
本回答被提问者采纳