gpt4 book ai didi

r - 尝试从网站读取 Excel 文件时出错

转载 作者:行者123 更新时间:2023-12-04 08:42:58 25 4
gpt4 key购买 nike

我正在尝试下载 xlsx可在以下网址获得的文件。如果您访问该网站并单击该链接,它将作为文件下载到您的计算机上。但是,我想自动化这个过程。我尝试了以下方法:

library(RCurl)
download.file("https://dshs.texas.gov/coronavirus/TexasCOVID19DailyCountyCaseCountData.xlsx", "temp.xlsx")
library(readxl)
tmp <- read_xlsx("temp.xlsx")
# Error: Evaluation error: error reading from the connection.
此方法确实将 temp.xlsx 文件下载到我的驱动器。但是,如果您尝试手动单击它以打开它,则 excel 无法打开它。它知道它的大小,但无法打开。
.
readxl::read_xlsx("https://dshs.texas.gov/coronavirus/TexasCOVID19DailyCountyCaseCountData.xlsx")
# Error: `path` does not exist: ‘https://dshs.texas.gov/coronavirus/TexasCOVID19DailyCountyCaseCountData.xlsx’
这两种方法都是我从网站下载 Excel 文件的首选方法。这些方法在这里不起作用有什么具体原因吗?

最佳答案

在 Windows 上下载某些文件格式时,您需要指定它应该是二进制文件而不是文本传输的(通常)默认值 - 来自 download.file()文档:

The choice of binary transfer (mode = "wb" or "ab") is important onWindows, since unlike Unix-alikes it does distinguish between text andbinary files and for text transfers changes \n line endings to \r\n(aka ‘CRLF’).

On Windows, if mode is not supplied (missing()) and url ends in one of.gz, .bz2, .xz, .tgz, .zip, .rda, .rds or .RData, mode = "wb" is setsuch that a binary transfer is done to help unwary users.

Code written to download binary files must use mode = "wb" (or "ab"),but the problems incurred by a text transfer will only be seen onWindows.


在这种情况下,为了正确写入文件,请使用:
download.file("https://dshs.texas.gov/coronavirus/TexasCOVID19DailyCountyCaseCountData.xlsx",
"temp.xlsx", mode = "wb")

关于r - 尝试从网站读取 Excel 文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64473524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com