如何使用Pipe/HTTP/CSV从互联网下载、提取和导入压缩或tgz-CSV文件



对于CSV文件,我使用PipeHTTPCSV的组合来下载,(最终(修改文件并将其作为DataFrame导入,而无需在磁盘上进行任何临时写入:

urlData = "https://github.com/sylvaticus/IntroSPMLJuliaCourse/raw/main/lessonsSources/02_-_JULIA2_-_Scientific_programming_with_Julia/data.csv"
data = @pipe HTTP.get(urlData).body                |>
replace!(_, UInt8(';') => UInt8(' ')) |>  # if we need to apply modifications to the file before importing 
CSV.File(_, delim=' ')                |>
DataFrame;

当文件已经用zip或tar压缩(假设存档中只有一个文件(时,我如何使用相同的通用方法(不保存临时磁盘的管道(?

例如:

urlDataZ = "https://github.com/sylvaticus/IntroSPMLJuliaCourse/raw/main/lessonsSources/02_-_JULIA2_-_Scientific_programming_with_Julia/data.zip"
urlDataT = "https://github.com/sylvaticus/IntroSPMLJuliaCourse/raw/main/lessonsSources/02_-_JULIA2_-_Scientific_programming_with_Julia/data.tgz"

对于压缩文件,我可以使用这种方法:

data = @pipe ZipFile.Reader("data.zip").files[1] |>
CSV.File(read(_), delim=';') |>
DataFrame  

但如果不在磁盘上传递一个明确的临时文件,我就无法将这三件事(下载、解压缩和导入(放在一起。

ZipFile.Reader也接受IO参数,因此您可以将内容包装在IOBuffer中,如下所示:

julia> @pipe HTTP.get(urlDataZ).body             |>
IOBuffer(_)                         |>
ZipFile.Reader(_).files[1]          |>
CSV.read(_, DataFrame, delim = ';')

8×4 DataFrame
Row │ Country  Year   forarea   forvol  
│ String7  Int64  Float64   Float64 
─────┼───────────────────────────────────
1 │ Germany   2000  11.354    3381.0
2 │ France    2000  15.288    2254.28
3 │ Italy     2000   8.36925  1058.71
4 │ Sweden    2000  28.163    3184.67
5 │ Germany   2020  11.419    3663.0
6 │ France    2020  17.253    3055.83
7 │ Italy     2020   9.56613  1424.4
8 │ Sweden    2020  27.98     3653.91

相关内容

最新更新