2

我不认为我正在编写的这段小代码有任何实际应用,但我正试图将我的头脑围绕 async 并且似乎有点挣扎。假设我想从 Yahoo 提取历史股票价格数据,将所有数据保存到单个 csv 文件中,然后使用批量复制将其加载到 SQL Server 中。我不太担心将数据加载到 SQL Server 中,但我想知道如何将数据写入新的 csv 文件。可以/应该异步完成吗?

据我所知,在抓取历史数据时,无法将代码作为流的一部分抓取,因此我正在抓取流并将其映射到一个新列表,并将代码附加到每个项目的前面。有时,当我运行测试时,我会得到一个没有代码的记录,并且会有一个包含多个代码的记录(例如,“MSFT,YHOO”)。

所以,我的问题是,如何将这些数据转储到单个 csv 文件中而不引起问题?其次,当我拆分数据时,我得到一个空的尾部项目。放弃它的最佳方法是什么?

就像我说的,我不知道这有什么实际应用,但我正在努力学习,所以我希望你能原谅我的无知。感谢您的帮助,我真的很感激。这是我所拥有的:

open System
open System.IO
open System.Web
open System.Net

let fromDate = new DateTime(2013, 1, 1)

let getTickers = 
    "MSFT" :: "YHOO" :: []

let getData (ticker : string) = 
    async {
        let url = System.String.Format("http://ichart.finance.yahoo.com/table.csv?s={0}&g=d&ignore=.csv&a={1}&b={2}&c={3}", ticker, fromDate.Month - 1, fromDate.Day, fromDate.Year)

        Console.WriteLine(url)

        let req = WebRequest.Create(url)
        let! rsp = req.AsyncGetResponse()
        use stream = rsp.GetResponseStream()
        use reader = new StreamReader(stream)

        let lines = 
            reader.ReadToEnd().Split('\n')
            |> Seq.skip 1 // skip header
            |> Seq.map (fun line-> (String.Format("{0}, {1}", ticker, line.ToString())))

        Seq.iter (fun x->printfn "%s" (x.ToString())) lines
        ()
    }

let z = 
    getTickers 
    |> List.map getData
    |> Async.Parallel
    |> Async.RunSynchronously
4

1 回答 1

2

IMO,这有点过头了,但希望它能证明你想知道的:

open System
open System.IO
open System.Net

let tickers = 
  [ 
    "MSFT"
    "YHOO"
  ]

let getData (writer: TextWriter) ticker =
  async {
    let url = sprintf "http://ichart.finance.yahoo.com/table.csv?s=%s&g=d&ignore=.csv" ticker
    let req = WebRequest.Create(url)
    let! resp = req.GetResponseAsync() |> Async.AwaitTask
    use stream = resp.GetResponseStream()
    use reader = new StreamReader(stream)
    let! lines = reader.ReadToEndAsync() |> Async.AwaitTask
    let lines = 
      lines.Split('\n')
        |> Seq.skip 1
        |> Seq.filter ((<>) "") //skip empty lines
    for line in lines do
      do! writer.WriteLineAsync(String.Format("{0}, {1}", ticker, line)).ContinueWith(ignore) |> Async.AwaitTask
  }

let writeAllToFile path =
  use writer = new StreamWriter(path=path)
  tickers
    |> Seq.map (getData writer)
    |> Async.Parallel
    |> Async.RunSynchronously
    |> ignore

writeAllToFile @"C:\quotes.csv"
于 2013-01-10T16:25:42.167 回答