我尝试使用“下载”和 CSV 从 Julia 中的网址获取一个简单的 csv 文件,但没有成功。 这是我到目前为止所做的:
using Downloads, CSV
url = "https://r-data.pmagunia.com/system/files/datasets/dataset-85141.csv"
f = Downloads.download(url)
df = CSV.read(f, DataFrame)
但我收到以下错误:ArgumentError:符号名称可能不包含\0
我尝试过使用标准化名称,但也没有成功:
f = Downloads.download(url)
df = CSV.File(f, normalizenames=true)
但随后我收到无效的 UTF-8 字符串错误消息。
当我简单地下载文件并使用 CSV.read 从我的电脑获取它时,我没有收到任何错误。
最佳答案
服务器使用 Content-Encoding: gzip
提供该文件,即传输的数据是压缩的,客户端需要对其进行解压缩。你可以自己在命令行尝试一下,curl默认不解压:
$ curl https://r-data.pmagunia.com/system/files/datasets/dataset-85141.csv [9:40:49]
Warning: Binary output can mess up your terminal. Use "--output -" to tell
Warning: curl to output it to your terminal anyway, or consider "--output
Warning: <FILE>" to save to a file.
但是,如果您传递 --compressed
标志:
$ curl --compressed https://r-data.pmagunia.com/system/files/datasets/dataset-85141.csv
"time","Nile"
1871,1120
1872,1160
1873,963
[...]
Downloads.jl 使用 libcurl,我在 Downloads.jl 存储库中找不到太多关于处理压缩内容的提及。
要暂时修复此问题,您可以升级到 CSV.jl v0.9.4,it handles gzipped CSV-files透明地。
如果无法更新,您可以手动使用 CodecZlib.jl:
using Downloads, CSV, DataFrames, CodecZlib
url = "https://r-data.pmagunia.com/system/files/datasets/dataset-85141.csv"
f = Downloads.download(url)
df = open(fh -> CSV.read(GzipDecompressorStream(fh), DataFrame), f)
关于csv - 下载 CSV 问题 Julia - ArgumentError : Symbol name may not contain\0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69382773/