【问题标题】:Import text file as single character string将文本文件作为单个字符串导入
【发布时间】:2012-02-22 12:20:33
【问题描述】:

如何在 R 中将纯文本文件作为单个字符串导入?我认为这可能会有一个非常简单的答案,但是当我今天尝试这个时,我发现我找不到执行此操作的函数。

例如,假设我有一个文件foo.txt,其中包含我想要文本挖掘的内容。

我试过了:

scan("foo.txt", what="character", sep=NULL)

但这仍然返回一个向量。我得到了它的一些工作:

paste(scan("foo.txt", what="character", sep=" "),collapse=" ")

但这是一个非常丑陋的解决方案,可能也不稳定。

【问题讨论】:

  • readr::read_file 现在很好地解决了这个问题。

标签: r


【解决方案1】:

如果 3 年后还有人在看这个问题,Hadley Wickham 的 readr 包有一个方便的 read_file() 函数可以帮你解决这个问题。

# you only need to do this one time on your system
install.packages("readr")
library(readr)
mystring <- read_file("path/to/myfile.txt")

【讨论】:

  • 唉“read_file”现在没有出现在 stringr 中。 :(cran.r-project.org/web/packages/stringr/stringr.pdf
  • @mlk 它已被迁移到readr。我已经相应地更新了答案 - 我希望莎伦不介意。
  • 不错!还可以即时解压缩 .gz 文件
  • 我在这段代码上得到了could not find function "pase"
【解决方案2】:

看来您的解决方案并不难看。您可以像这些方式使用函数并使其专业化

  • 第一路
new.function <- function(filename){
  readChar(filename, file.info(filename)$size)
}

new.function('foo.txt')
  • 第二种方式
new.function <- function(){
  filename <- 'foo.txt'
  return (readChar(filename, file.info(filename)$size))
}

new.function()

【讨论】:

  • 这不会对@Tommy 提供的答案添加任何内容。在函数环境中提供路径是特别糟糕的解决方案。
【解决方案3】:

readr 包有一个功能可以为你做任何事情。

install.packages("readr") # you only need to do this one time on your system
library(readr)
mystring <- read_file("path/to/myfile.txt")

这将替换包 stringr 中的版本。

【讨论】:

    【解决方案4】:

    太糟糕了,Sharon 的解决方案不能再使用了。我已将 Josh O'Brien 的解决方案与 asieira 的修改添加到我的 .Rprofile 文件中:

    read.text = function(pathname)
    {
        return (paste(readLines(pathname), collapse="\n"))
    }
    

    并像这样使用它:txt = read.text('path/to/my/file.txt')。我无法复制土包子(2014 年 10 月 28 日)的发现,writeLines(txt) 显示了file.txt 的内容。此外,在write(txt, '/tmp/out') 命令diff /tmp/out path/to/my/file.txt 之后报告没有差异。

    【讨论】:

      【解决方案5】:

      readChar 没有太大的灵活性,所以我结合了您的解决方案(readLines 和粘贴)。

      我还在每行之间添加了一个空格:

      con <- file("/Users/YourtextFile.txt", "r", blocking = FALSE)
      singleString <- readLines(con) # empty
      singleString <- paste(singleString, sep = " ", collapse = " ")
      close(con)
      

      【讨论】:

        【解决方案6】:

        这是来自@JoshuaUlrich 的解决方案的变体,它使用正确的大小而不是硬编码的大小:

        fileName <- 'foo.txt'
        readChar(fileName, file.info(fileName)$size)
        

        请注意,readChar 会为您指定的字节数分配空间,因此readChar(fileName, .Machine$integer.max) 无法正常工作...

        【讨论】:

        • 值得指出的是,此代码不适用于压缩文件。在这种情况下,file.info(filename)$size 返回的字节数将与将在内存中读取的实际内容不匹配,我们预计会更大。
        【解决方案7】:

        怎么样:

        string <- readChar("foo.txt",nchars=1e6)
        

        【讨论】:

        • 这个 1e6 大小是 readChar fun 限制还是 R 内存限制?谢谢
        • @TomásNavarro:两者都不是,这是对文件中有多少字节的高估。接受的答案使用file.info() 来获取文件中的实际字节数。
        【解决方案8】:

        我会使用以下内容。它应该工作得很好,而且看起来并不难看,至少对我来说:

        singleString <- paste(readLines("foo.txt"), collapse=" ")
        

        【讨论】:

        • 我原以为 collapse="\n" 会复制这些是原始文件中单独的行的事实。通过此更改,此解决方案同样适用于压缩和未压缩文件。
        • 这似乎不起作用。如果我 writeLines(singleString),我会得到一个损坏的文件...
        • 如果最后一行不包含行尾字符,这将不起作用。在这种情况下,最后一行不包含在字符串中(或者,文件在最后一个换行符处被截断)。
        • 这可以很好地读取文本文件,就像在 OP 的问题中一样:文本文件连接默认为 blocking=TRUE,因此 readLines() 将返回完整文件,并带有关于缺少 EOL 字符的警告。不过@gvrocha 的评论值得关注:了解您的连接类型! ?readLines help 说If the final line is incomplete (no final EOL marker) the behaviour depends on whether the connection is blocking or not. For a non-blocking text-mode connection the incomplete line is pushed back, silently. **For all other connections the line will be accepted, with a warning.**
        猜你喜欢
        • 1970-01-01
        • 2015-07-30
        • 1970-01-01
        • 2014-03-06
        • 2018-11-07
        • 2021-02-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多