是否有一种从一些网址获取数据的相当简单的方法?我试过最明显的版本,不起作用:
readcsv("https://dl.dropboxusercontent.com/u/.../testdata.csv")
我没有找到任何有用的参考资料。有什么帮助吗?
答案 0 :(得分:19)
如果您想从网址中读取CSV,可以将Requests package用作@waTeim shows,然后通过IOBuffer阅读数据。见下面的例子。
或者,作为@Colin T Bowers条评论,您可以使用当前(2017年12月)更积极维护的HTTP.jl评论包:
julia> using HTTP
julia> res = HTTP.get("https://www.ferc.gov/docs-filing/eqr/q2-2013/soft-tools/sample-csv/transaction.txt");
julia> mycsv = readcsv(res.body);
julia> for (colnum, myheader) in enumerate(mycsv[1,:])
println(colnum, '\t', myheader)
end
1 transaction_unique_identifier
2 seller_company_name
3 customer_company_name
4 customer_duns_number
5 tariff_reference
6 contract_service_agreement
7 trans_id
8 transaction_begin_date
9 transaction_end_date
10 time_zone
11 point_of_delivery_control_area
12 specific location
13 class_name
14 term_name
15 increment_name
16 increment_peaking_name
17 product_name
18 transaction_quantity
19 price
20 units
21 total_transmission_charge
22 transaction_charge
使用Requests.jl
包:
julia> using Requests
julia> res = get("https://www.ferc.gov/docs-filing/eqr/q2-2013/soft-tools/sample-csv/transaction.txt");
julia> mycsv = readcsv(IOBuffer(res.data));
julia> for (colnum, myheader) in enumerate(mycsv[1,:])
println(colnum, '\t', myheader)
end
1 transaction_unique_identifier
2 seller_company_name
3 customer_company_name
4 customer_duns_number
5 tariff_reference
6 contract_service_agreement
7 trans_id
8 transaction_begin_date
9 transaction_end_date
10 time_zone
11 point_of_delivery_control_area
12 specific location
13 class_name
14 term_name
15 increment_name
16 increment_peaking_name
17 product_name
18 transaction_quantity
19 price
20 units
21 total_transmission_charge
22 transaction_charge
答案 1 :(得分:9)
Requests包看起来效果很好。还有其他人(参见整个package list),但积极维护请求。
julia> Pkg.add("Requests")
julia> using Requests
您可以使用与各种HTTP动词获取,发布等对应的导出函数之一,它返回响应类型< / p>
julia> res = get("http://julialang.org")
Response(200 OK, 21 Headers, 20913 Bytes in Body)
julia> typeof(res)
Response (constructor with 8 methods)
然后,例如,您可以使用 @printf
打印数据julia> @printf("%s",res.data);
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
...
答案 2 :(得分:2)
如果它直接是一个csv文件,这样的东西应该可以工作:
function getAllInstructions(req, res) {
return new Promise(function(resolve, reject) {
connection.query('SELECT * FROM instructions where id = ?', [req.params.id], function(err, rows, fields) {
if (err) {
reject(err);
} else {
var instructionPromises = rows.map(function(row) {
var instruction = new Instruction();
instruction.id = row.id;
instruction.title = row.title;
return getStepsForInstruction(instruction.id).then(function(steps) {
instruction.steps = steps;
return instruction;
});
});
resolve(Promise.all(instructionPromises));
}
});
});
}
// (getStepsForInstruction is like my earlier one)
// Usage:
getAllInstructions(req, res).then(function(instructions) {
// They're all done, render them here
});
答案 3 :(得分:1)
如果您希望读入数据框,这在Julia中也可以使用:
using CSV
dataset = CSV.read(download("https://mywebsite.edu/ml/machine-learning-databases/my.data"))
答案 4 :(得分:1)
如今,您还可以使用UrlDownload.jl,它是纯Julia,可以处理下载详细信息,在内存中处理数据,还可以处理压缩文件。
用法很简单
using UrlDownload
A = urldownload("https://data.ok.gov/sites/default/files/unspsc%20codes_3.csv")