【发布时间】: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 代码中,即<TEXT> 和 </TEXT>。
我想以一种有四列的方式将其读入 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 解析要复杂一些。您需要一个类似于我在下面显示的正则表达式解决方案来解析数据。祝你好运。