CSV数据来自URL。

huangapple go评论56阅读模式
英文:

CSV data from URL

问题

以下是翻译好的部分:

Data

以下是美联储资产负债表资产的图表:

CSV数据来自URL。

链接:

https://fred.stlouisfed.org/series/WALCL

Script

以下的 F# 脚本将检索系列数据并显示它:

#r "nuget: CsvHelper, 30.0.1"

open System.IO
open System.Net.Http
open CsvHelper

type WALCLRecord() =
    member val DATE = "" with get, set
    member val WALCL = "" with get, set

let uri = sprintf "https://fred.stlouisfed.org/graph/fredgraph.csv?id=%s&cosd=%s" "WALCL" "2023-01-01"
        
let str = (new HttpClient()).GetStringAsync(uri).Result

use string_reader = new StringReader(str)
use csv_reader = new CsvReader(string_reader, System.Globalization.CultureInfo.InvariantCulture)

let records = csv_reader.GetRecords<WALCLRecord>()

let arr = records |> Array.ofSeq

for elt in arr do
    printfn "%10s %15s" elt.DATE elt.WALCL

结果:

2023-03-15       8639300.0                                                                                                                                                                               
2023-03-22       8733787.0                                                                                                                                                                               
2023-03-29       8705942.0                                                                                                                                                                               
2023-04-05       8632384.0                                                                                                                                                                               
2023-04-12       8614797.0                                                                                                                                                                               
2023-04-19       8593263.0                                                                                                                                                                               
2023-04-26       8562768.0                                                                                                                                                                               
2023-05-03       8503994.0                                                                                                                                                                               
2023-05-10       8503017.0                                                                                                                                                                               
2023-05-17       8456760.0                                                                                                                                                                               
2023-05-24       8436255.0

Question

我使用了 CsvHelper,因为那是我熟悉的工具。

然而,我看到 FSharp.Data 很受欢迎并且支持 CSV。

是否有一种方法可以使用 FSharp.Data 实现上述功能,而不必通过临时文件指定格式?该网站上的示例使用了类型提供程序,并在文件中提供了示例数据。我希望避免在文件中提供示例数据。

此外,在上述脚本中,我使用了具有 WALCLRecord 属性的类,以使其与 CsvHelper 兼容。我更喜欢使用类似于以下的内容:

type WALCLRecord = {
    DATE : string
    WALCL : float
}

但 CsvHelper 似乎不支持这种方式。这是我希望查看 FSharp.Data 是否可行的另一个原因。

英文:

Data

Here's a chart of the Federal Reserve Balance sheet assets:

CSV数据来自URL。

Link:

https://fred.stlouisfed.org/series/WALCL

Script

The following F# script will retrieve the series data and display it:

#r &quot;nuget: CsvHelper, 30.0.1&quot;

open System.IO
open System.Net.Http
open CsvHelper

type WALCLRecord() =
    member val DATE = &quot;&quot; with get, set
    member val WALCL = &quot;&quot; with get, set

let uri = sprintf  &quot;https://fred.stlouisfed.org/graph/fredgraph.csv?id=%s&amp;cosd=%s&quot; &quot;WALCL&quot; &quot;2023-01-01&quot;
        
let str = (new HttpClient()).GetStringAsync(uri).Result

use string_reader = new StringReader(str)
use csv_reader = new CsvReader(string_reader, System.Globalization.CultureInfo.InvariantCulture)

let records = csv_reader.GetRecords&lt;WALCLRecord&gt;()

let arr = records |&gt; Array.ofSeq

for elt in arr do
    printfn &quot;%10s %15s&quot; elt.DATE elt.WALCL

Result:

2023-03-15       8639300.0                                                                                                                                                                               
2023-03-22       8733787.0                                                                                                                                                                               
2023-03-29       8705942.0                                                                                                                                                                               
2023-04-05       8632384.0                                                                                                                                                                               
2023-04-12       8614797.0                                                                                                                                                                               
2023-04-19       8593263.0                                                                                                                                                                               
2023-04-26       8562768.0                                                                                                                                                                               
2023-05-03       8503994.0                                                                                                                                                                               
2023-05-10       8503017.0                                                                                                                                                                               
2023-05-17       8456760.0                                                                                                                                                                               
2023-05-24       8436255.0          

Question

I used CsvHelper because that's what I'm familiar with.

However, I see that FSharp.Data is popular and has CSV support.

Is there a way to implement the above using FSharp.Data and not have to go through a temporary file to specify the format? The example on the site uses the type provider with example data in a file. I'd like to avoid having to provide example data in a file.

Also, in the above script, I used a class with properties for WALCLRecord in order to be compatible with CsvHelper. I'd prefer to use something like this instead:

type WALCLRecord = {
    DATE : string
    WALCL : float
}

but CsvHelper didn't play well with that. So that's another reason I'd like to see if FSharp.Data is an option.

答案1

得分: 3

关于类型提供程序的好处是它们可以自动为您生成WALCLRecord类型,因此您不必自己定义它。这对于简单的脚本非常方便(对于更大的应用程序,您可能希望拥有一个完全受控的领域模型)。

使用F#数据的解决方案如下:

#r "nuget:FSharp.Data"
open FSharp.Data

// 生成一个提供的类型,基于示例URL
type StFedCsv = CsvProvider<"https://fred.stlouisfed.org/graph/fredgraph.csv?id=WALCL&cosd=2023-01-01">

// 在运行时加载实际数据,可能使用不同的URL(但格式相同)
let records = StFedCsv.Load(sprintf "https://fred.stlouisfed.org/graph/fredgraph.csv?id=%s&cosd=%s" "WALCL" "2023-01-01")

// 遍历行 - 注意`DATE`被推断为`DateTime`,`WALCL`被推断为`decimal`
for elt in records.Rows do
    printfn "%A %A" elt.DATE elt.WALCL

希望这对您有帮助。

英文:

The nice thing about type providers is that they can automatically generate the WALCLRecord type for you, so you do not have to define it. This is great for simple scripts (for larger applications, you'd want to have a domain model that you fully control, though).

A solution using F# data looks like this

#r &quot;nuget:FSharp.Data&quot;
open FSharp.Data

// Generate a provided type, based on a sample URL
type StFedCsv = CsvProvider&lt;&quot;https://fred.stlouisfed.org/graph/fredgraph.csv?id=WALCL&amp;cosd=2023-01-01&quot;&gt;

// Load actual data at runtime, using possibly 
// different URL (with the same format)
let records = StFedCsv.Load(sprintf  &quot;https://fred.stlouisfed.org/graph/fredgraph.csv?id=%s&amp;cosd=%s&quot; &quot;WALCL&quot; &quot;2023-01-01&quot;)

// Iterate over the rows - not that `DATE` is inferred 
// as `DateTime` and `WALCL` is inferred as `decimal`
for elt in records.Rows do
    printfn &quot;%A %A&quot; elt.DATE elt.WALCL

huangapple
  • 本文由 发表于 2023年5月29日 10:39:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76354391.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定