using HTTP, ZipFile, CSV
datafile="ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/Datasets/DVS/natality/Nat2018ps.zip"
function rzip(datafile)
dat = HTTP.get(datafile)
r = ZipFile.Reader(IOBuffer(dat.body))
f = r.files[1]
CSV.read(f, delim=' ', ignorerepeated=true)
end
rzip
函数读取其中的 zipfile 和 txt 文件,使用 CSV
创建数据帧,然后将其读入表中。
运行时出现以下错误:
julia> rzip(datafile)
ERROR: BoundsError: attempt to access 16-element Array{UInt64,1} at index [-9223372036854775807]
最佳答案
ZipFile.Reader
流不是随机访问流,因此它不能在最近在 CSV.jl
中引入的多线程中正常工作
因此,您需要使用 threaded=false
选项。
using HTTP, ZipFile, CSV
datafile="ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/Datasets/DVS/natality/Nat2018ps.zip"
dat = HTTP.get(datafile)
r = ZipFile.Reader(IOBuffer(dat.body))
f = r.files[1]
df = CSV.read(f, delim=' ', ignorerepeated=true, threaded=false)
现在只是为了证明它有效:
julia> df
25918×56 DataFrames.DataFrame. Omitted printing of 51 columns
│ Row │ 201801 │ 04272GU │ 010311 │ 1 │ 20083US │
│ │ Int64 │ String │ Int64 │ Int64 │ String │
├───────┼────────┼─────────┼────────┼───────┼─────────┤
│ 1 │ 201801 │ 05592GU │ 10311 │ 1 │ 35116FM │
│ 2 │ 201801 │ 11362GU │ 10311 │ 1 │ 22083US │
⋮
│ 25916 │ 201808 │ 01001PR │ 31371 │ 2 │ 22083US │
│ 25917 │ 201811 │ 00495PR │ 21311 │ 1 │ 19072US │
│ 25918 │ 201806 │ 10221PR │ 127211 │ 1 │ 19072US │
关于zip - 从 ftp url 导入 zip 文件中的文本 CSV 文件会导致绑定(bind)错误 (BoundsError),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59601347/