如何下载和解析csv文件在球拍? [英] How to download and parse a csv file in Racket?
本文介绍了如何下载和解析csv文件在球拍?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Racket中下载并解析csv文件?
How do I download and parse a csv file in Racket?
推荐答案
使用 get-pure-port
下载文件,并使用 Planet 图书馆 (require(planet neil / csv)
)解析。
Use get-pure-port
to download the file, and use the Planet library (require (planet neil/csv)
) to parse it.
以下示例下载并解析一个csv文件,其中包含了关于各种加拉帕戈斯群岛大小以及每个岛屿上发现了多少种类的
数据。
The following example downloads and parses a csv file containing data on the size of the various Galapagos islands and how many species were found on each island.
#lang racket
(require (planet neil/csv:1:=7) net/url)
(define galapagos-url
(string->url
"http://www.stat.washington.edu/~handcock/536/Data/galapagos.csv"))
(define make-galapagos-csv-reader
(make-csv-reader-maker
'((separator-chars #\,)
(strip-leading-whitespace? . #t)
(strip-trailing-whitespace? . #t))))
(define (all-rows url make-reader)
(define next-row (make-reader (get-pure-port url)))
(define (loop)
(define row (next-row))
(if (empty? row)
'()
(cons row (loop))))
(loop))
(all-rows galapagos-url make-galapagos-csv-reader)
返回的第一行是:
'(("Island"
"Observed.species"
"Native.species"
"Area(km^2)"
"Elevation(m)"
"Distance.nearest.island(km)"
"Distance.Santa.Cruz(km)"
"Area.adj.island(km^2)")
("Baltra" "58" "23" "25.09" "" "0.6" "0.6" "1.84")
("Bartolome" "31" "21" "1.24" "109" "0.6" "26.3" "572.33")
("Caldwell" "3" "3" "0.21" "114" "2.8" "58.7" "0.78")
这篇关于如何下载和解析csv文件在球拍?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文