2017-02-25 82 views
0

我想在线下载excel文件并只读取包含单词“ORD”的行。从Excel文件在线导入数据

fileUrl <-("http://www.hkexnews.hk/reports/sharerepur/documents/SRRPT20151211.xls") 
x <- getURLContent(fileUrl) 
out <- read.table(fileUrl,x) 

我使用GetUrlContent但在过程的早期阶段收到一个错误:

警告消息:

1: In read.table(fileUrl, x) : line 1 appears to contain embedded nulls 
2: In read.table(fileUrl, x) : line 2 appears to contain embedded nulls 
3: In read.table(fileUrl, x) : line 3 appears to contain embedded nulls 
4: In read.table(fileUrl, x) : line 4 appears to contain embedded nulls 
5: In read.table(fileUrl, x) : line 5 appears to contain embedded nulls 
6: In if (!header) rlabp <- FALSE : 
    the condition has length > 1 and only the first element will be used 
7: In if (header) { : 
    the condition has length > 1 and only the first element will be used 
8: In scan(file = file, what = what, sep = sep, quote = quote, dec = dec, : embedded nul(s) found in input 

表 “走出去” 出来几乎不可读。有谁知道如何精确读取特定的行,而不是导入整个文件的风险得到错误行?

回答

1

其中this SO question的答案建议使用gdata库从网上下载Excel文件,然后使用read.xls()将其读入数据框。事情是这样的:

library(gdata) 
download.file("http://www.hkexnews.hk/reports/sharerepur/documents/SRRPT20151211.xls", destfile="file.xls") 
out <- read.xls("file.xls", header=TRUE, pattern="Some Pattern") 

pattern标志告诉read.xls()直到第一线Some Pattern出现在其中不顾一切。您可以将该值更改为允许您在数据框中的实际数据之前跳过初步材料的值。

+0

喜添,非常感谢你。我可以检索文件没有任何问题。但问题是该文件不完全是数据框。它包含一些描述性的信息,直到某一行,然后有一个数据帧。 – Pino

+0

@Pino我更新了我的答案。使用'pattern'跳过你不想要的行。 –

1

我只是找到了一个解决方案,谢谢你蒂姆把我在正确的方向: library(gdata) DownloadURL <- "http://www.hkexnews.hk/reports/sharerepur/documents/SRRPT20151211.xls" out <- read.xls(DownloadURL, pattern="ORD", perl = "C:\\Perl64\\bin\\perl.exe")