zip - 从 ftp url 导入 zip 文件中的文本 CSV 文件会导致绑定(bind)错误 (BoundsError)

标签 zip julia

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/

相关文章:

ruby-on-rails - Rails 使用 send_data 或 send_file 将多个文件发送到浏览器

types - 如何使用 Flux.jl 绘制函数及其梯度/导数

python - 如何使用 Boto3 Python 在 S3 中创建 zipfile?

node.js - Node.js 是否有一个 zip 存档创建/解压模块可以运行/维护?

julia - 如何在 Julia 中将矩阵漂亮地打印到字符串?

julia - 如何检查文件是否在 Julia 中无异常(exception)地存在?

julia - 在 Julia 中评估与矢量化函数的组合

arrays - 如何在 Julia 中交错数组

java - 使用Gradle将目录压缩到多个目的地

Php 保留从 zip 中提取的文件的修改日期