我有一个包含html代码的文本文件。导入时导致错误

时间:2019-04-17 19:16:39

标签: r read.table

我正在尝试导入一个包含html代码的文本文件。我正在尝试使用read.table进行导入,并以竖线(〜)分隔。

文本文件如下所示:

id~title~content
Article-123~Title 1~<h2>Overview of Article 1</h2>

<p>This is the content of article 123.</p>
Article-456~Title 2~<h1>Problem:</h1><br>
<br>
This is the content of article 456
Article-789~Title 3~<h1>This is the content of article 789 </h1>

我正在使用的代码使我接近:

text <- read.table("filepath/text_file.txt",
                    quote = "\"",
                    sep = "~",
                    fill = TRUE,
                    header = TRUE,
                    comment.char = "",
                    stringsAsFactors = TRUE,
                    na.strings = "\\n",
                    allowEscapes = FALSE)

我得到:

id              title       content
Article-123     Title 1     <h2>Overview of Article 1</h2>
Article-456     Title 2     <h1>Problem:</h1><br>
<br>
Article-567     Title 3     <h1>This is the content of article 789 </h1>

当我导入R时,似乎HTML正在添加换行符。相反,我希望导入看起来像这样:

id              title       content
Article-123     Title 1     <h2>Overview of Article 1</h2>
Article-456     Title 2     <h1>Problem:</h1><br>
Article-567     Title 3     <h1>This is the content of article 789 </h1>

2 个答案:

答案 0 :(得分:0)

您可以查看是否可行?我不确定要用read.table来解释某些换行而不是其他换行的方法(您如何知道换行是否意味着新行?)相反,我们可以尝试以下方法:

  1. 以行的形式读取数据(因此文本的每一行都是字符向量的元素)
  2. 通过查找~字符来找出每一行属于哪些行,然后折叠这些行并替换换行符。如果HTML的任何地方都包含~,则可能会很脆弱。
  3. 使用separate将新整理的行分为三列。
library(tidyverse)
text <- "id~title~content
Article-123~Title 1~<h2>Overview of Article 1</h2>

<p>This is the content of article 123.</p>
Article-456~Title 2~<h1>Problem:</h1><br>
<br>
This is the content of article 456
Article-789~Title 3~<h1>This is the content of article 789 </h1>"

text_in <- read_lines(text) %>%
  tibble(line = .) %>%
  mutate(row = str_detect(line, "~") %>% cumsum) %>%
  group_by(row) %>%
  summarise(line = str_c(line, collapse = "\n")) %>%
  separate(line, into = c("id", "title", "content"), sep = "~") %>%
  slice(-1)

text_in
#> # A tibble: 3 x 4
#>     row id        title   content                                          
#>   <int> <chr>     <chr>   <chr>                                            
#> 1     2 Article-… Title 1 "<h2>Overview of Article 1</h2>\n\n<p>This is th…
#> 2     3 Article-… Title 2 "<h1>Problem:</h1><br>\n<br>\nThis is the conten…
#> 3     4 Article-… Title 3 <h1>This is the content of article 789 </h1>

reprex package(v0.2.1)于2019-04-17创建

答案 1 :(得分:0)

如果正在使用data.tables,则可以尝试此操作。我的方法有以下假设:

  1. 如果列("title""content")具有NA,则该行是<br>comment<p>
  2. 该文本文件中将包含更多这些行

基于这些假设,如果您使用library(readr),它将创建一个tibble表,您可以将其设置回data.table,同时使用NA删除任何行。

这是代码:

text <- "id~title~content
Article-123~Title 1~<h2>Overview of Article 1</h2>

<p>This is the content of article 123.</p>
Article-456~Title 2~<h1>Problem:</h1><br>
<br>
This is the content of article 456
Article-789~Title 3~<h1>This is the content of article 789 </h1>"

library(readr)
library(data.table)
test <- na.omit(setDT(read_delim(text, delim = "~")))

test


            id   title                                      content
1: Article-123 Title 1               <h2>Overview of Article 1</h2>
2: Article-456 Title 2                        <h1>Problem:</h1><br>
3: Article-789 Title 3 <h1>This is the content of article 789 </h1>

我添加了此内容是因为我喜欢使用data.tables,因此使用fread您还可以执行以下操作:

library(data.table)
test <- na.omit(fread(text,header = TRUE, sep = "~", 
                      na.strings = "", fill = TRUE, 
                      blank.lines.skip = TRUE))


test
            id   title                                      content
1: Article-123 Title 1               <h2>Overview of Article 1</h2>
2: Article-456 Title 2                        <h1>Problem:</h1><br>
3: Article-789 Title 3 <h1>This is the content of article 789 </h1>