简体   繁体   English

F#处理CSV,按记录解析记录

[英]F# processing csv, parsing record by record

I'm new to F# basically i'm attempting to use the FileHelpers library to import the csv record by record. 我基本上是F#的新手,我正在尝试使用FileHelpers库按记录导入csv记录。 So i can then place each object in a queue. 这样我便可以将每个对象放入队列中。

let importOrders (filePath:string) = do
       let engine = new FileHelpers.FileHelperAsyncEngine(typeof<DataTypes.Order.OrderType>)
       engine.BeginReadFile(filePath)
       for o in engine do
         unProccessedData.Enqueue(o)

However its saying that engine is not a type who's value can be enumerated yet it implements IEnumerable. 然而,俗话说引擎不是可以枚举值的类型,但它实现了IEnumerable。 Anyone got any suggestions on a possible fix or a different approach. 任何人都对可能的修复方法或其他方法提出了任何建议。

I need to be able to process the csv record by record, converting each into the orderType and then storing the order onto the queue. 我需要能够通过记录处理csv记录,将每个记录转换为orderType,然后将订单存储到队列中。 Thanks for the help guys 谢谢你们的帮助

The type of engine (which is called FileHelperAsyncEngine ) only implements the non-generic version of IEnumerable , but it does not implement generic IEnumerable<T> which is required by the F# compiler. engine类型(称为FileHelperAsyncEngine )仅实现IEnumerable的非通用版本,但未实现F#编译器所需的通用IEnumerable<T>

You'll need to cast the non-generic IEnumerable to generic IEnumerable<obj> (or to some other instantiation of the generic type, depending on what is the type of values in the collection). 您需要将非泛型IEnumerable为泛型IEnumerable<obj> (或泛型的其他一些实例化,具体取决于集合中值的类型)。 The following should do the trick: 以下应该可以解决问题:

let importOrders (filePath:string) = 
    let engine = new FileHelpers.FileHelperAsyncEngine(typeof<OrderType>) 
    engine.BeginReadFile(filePath) 
    for o in engine |> Seq.cast<obj> do // NOTE: Conversion using Seq.cast
        unProccessedData.Enqueue(o) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM