【问题标题】:error reading text file into new columns of a dataframe using some text editing使用某些文本编辑将文本文件读入数据框的新列时出错
【发布时间】:2017-09-24 05:10:54
【问题描述】:

我有一个文本文件 (0001.txt),其中包含如下数据:

<DOC> 
<DOCNO>1100101_business_story_11931012.utf8</DOCNO> 
<TEXT> 

The Telegraph - Calcutta (Kolkata) | Business | Local firms go global
                                                                                                                           6                                                                                      Local firms go global
          JAYANTA ROY CHOWDHURY                              
    New Delhi, Dec. 31: Indian companies are stepping out of their homes to try their luck on foreign shores.         
    Corporate India invested $2.7 billion abroad in the first quarter of 2009-2010 on top of $15.9 billion in 2008-09.         
    Though the first-quarter investment was 15 per cent lower than what was invested in the same period last year, merchant banker Sudipto Bose said, It marks a confidence in a new world order where Indian businesses see themselves as equal to global players.        
    According to analysts, confidence in global recovery, cheap corporate buys abroad and easier rules governing investment overseas had spurred flow of capital and could see total investment abroad top $12 billion this year and rise to $18-20 billion next fiscal.         
    For example, Titagarh Wagons plans to expand abroad on the back of the proposed Asian railroad project.        
    We plan to travel all around the world with the growth of the railroads, said Umesh Chowdhury of Titagarh Wagons.        
    India is full of opportunities, but we are all also looking at picks abroad, said Gautam Mitra, managing director of Indian Structurals Engineering Company.         
    Mitra plans to open a holding company in Switzerland to take his business in structurals to other Asian and African countries.         
    Indian companies created 3 lakh jobs in the US, while contributing $105 billion to the US economy between 2004 and 2007, according to commerce ministry statistics. During 2008-09, Singapore, the Netherlands, Cyprus, the UK, the US and Mauritius together accounted for 81 per cent of the total outward investment.        
    Bose said, And not all of it is organic growth. Much of our investment abroad reflects takeovers and acquisitions.         
    In the last two years, Suzlon acquired Portugals Martifers stake in German REpower Systems for $122 million. McNally Bharat Engineering has bought the coal and minerals processing business of KHD Humboldt Wedag. ONGC bought out Imperial Energy for $2 billion.         
    Indias foreign assets and liabilities today add up to more than 60 per cent of its gross domestic product. By the end of 2008-09, total foreign investment was $67 billion, more than double of that at the end of March 2007.                                                                                                                                       
</TEXT> 
</DOC>

以上,所有文本数据都在文本的 HTML 代码中,即
&lt;TEXT&gt;&lt;/TEXT&gt;

我想以一种有四列的方式将其读入 R 数据帧,并且数据应读取为:

Title                               Author                 Date      Text   
The Telegraph - Calcutta (Kolkata)  JAYANTA ROY CHOWDHURY  Dec. 31   Indian companies are stepping out of their homes to try their luck on foreign shores. Corporate India invested $2.7 billion abroad in the first quarter of 2009-2010 on top of $15.9 billion in 2008-09. Though the first-quarter investment was 15 percent lower than what was invested in the same period last year, merchant banker Sudipto Bose said, It marks a confidence in a new world order where Indian businesses see themselves as equal to global players.

我尝试使用 dplyr 阅读的内容如下所示:

 # read text file
 library(dplyr)
 library(readr) 

 dat <- read_csv("0001.txt") %>% slice(-8)

 # print part of data frame 
 head(dat, n=2)

在上面的代码中,我尝试从包含上述文本的文本文件中跳过前几行(这并不重要),然后将其读入数据框。

但我无法得到我正在寻找的东西,并且对我所做的事情感到困惑。

有人可以帮忙吗?

【问题讨论】:

  • Csv 需要某种分隔符来分隔字段,通常是逗号。每行是由一个或多个字段组成的数据记录。您的数据似乎没有这样的结构。
  • 我不认为数据框是包含该数据的最佳结构。您是否考虑过以DOCNO 作为名称的列表?此外,如果这是 html,您可能需要使用 html 解析包来帮助您阅读它。
  • 我同意@RichScriven。如果数据已经在 html 中,那么 html 解析包会更好地完成这项任务。您可以尝试 rvest、xml 和/或 xml2 等软件包。但是如果数据是由其他人收集的并且已经在文本文件中,那么您也可以使用正则表达式来解析字符串,类似于我在下面显示的内容。
  • @RyanRunge 数据是由其他人收集的,我只得到 TEXT 格式 :( 这超出了我的限制,不能做任何事情。对不起。
  • @MadhuSareen - 我明白了。你想要做的比 html 解析要复杂一些。您需要一个类似于我在下面显示的正则表达式解决方案来解析数据。祝你好运。

标签: r dataframe dplyr


【解决方案1】:

为了能够将数据作为数据框或表读入 R,数据需要具有由分隔符维护的一致结构。最常见的格式之一是带有逗号分隔值 (CSV) 的文件。

您正在使用的数据没有分隔符。它本质上是一个具有最小强制结构的字符串。因此,听起来这个问题与正则表达式 (regex) 和数据挖掘的关系比与将文本文件读入 R 更相关。因此,如果您经常执行此任务,我建议您研究这两件事。

除此之外,要在此示例中执行您想要的操作,我建议先将文本文件作为单个文本字符串读入 R。然后您可以使用正则表达式解析您想要的数据。以下是如何做到这一点的基本粗略草稿:

fileName <- "Path/to/your/data/0001.txt"

string <- readChar(fileName, file.info(fileName)$size)

df <- data.frame(
      Title=sub("\\s+[|]+(.*)","",string),
      Author=gsub("(.*)+?([A-Z]{2,}.*[A-Z]{2,})+(.*)","\\2",string),
      Date=gsub("(.*)+([A-Z]{1}[a-z]{2}\\.\\s[0-9]{1,2})+(.*)","\\2",string),
      Text=gsub("(.*)+([A-Z]{1}[a-z]{2}\\.\\s[0-9]{1,2})+[: ]+(.*)","\\3",string))

输出:

str(df)
'data.frame':   1 obs. of  4 variables:
 $ Title : chr "The Telegraph - Calcutta (Kolkata)"
 $ Author: chr "JAYANTA ROY CHOWDHURY"
 $ Date  : chr "Dec. 31"
 $ Text  : chr "Indian companies are stepping out of their homes to"| __truncated__

正则表达式之所以有用是因为它允许在字符串中使用非常特定的模式。缺点是当您使用不断变化的格式的字符串时。这可能意味着对所使用的正则表达式进行一些细微的调整。

【讨论】:

  • 瑞恩,我看到了一个问题。假设如果 Text 中没有 ` : `,那么“Text”可能不会得到正确的文本。相反,我猜一切都会进入作者或日期。您能否进行逻辑检查是否没有':'我仍然在文本列中得到正确的文本。
  • @MadhuSareen - 没问题。我们不需要添加太多来完成这项工作。我在上面所做的只是将“[:]”更改为“[:]”(我添加了一个空格)。这类似于对字符串中是否存在“:”的逻辑检查。不管有没有,上面的代码现在应该能够正确地提取正文。
  • @MadhuSareen - The other question you started 在 SO 上回答这个问题是不必要的。所有这些都可以用正则表达式来解决,不需要 if else 语句等。请参阅this reference R 中的正则表达式符号。
  • 我同意 Ryan 但仍然想在这里提出这一点,因为否则可能会有问题。 (实际上我正面临这样的问题)。不管怎么说,多谢拉。 :)
  • @MadhuSareen - 不用担心。我记得开始学习正则表达式是什么感觉。一开始可能会令人困惑。所以我只是想在正确的方向提出提示,以帮助您和其他人更顺利地学习事物。祝你好运:)
【解决方案2】:

read.table( file = ... , sep = "|") 将解决您的问题。

【讨论】:

  • 我收到以下错误 `dat % slice(-8) Error in scan(file = file , what = what, sep = sep, quote = quote, dec = dec, : 第 1 行没有 3 个元素'
猜你喜欢
  • 2012-01-10
  • 1970-01-01
  • 1970-01-01
  • 2013-03-26
  • 2021-07-01
  • 2010-12-26
  • 2018-06-13
  • 1970-01-01
  • 2021-12-22
相关资源
最近更新 更多