Parsing F#解析CSV文件-查找哪一行错误

Parsing F#解析CSV文件-查找哪一行错误,parsing,csv,f#,filehelpers,Parsing,Csv,F#,Filehelpers,我从找到的一些资源中编写了这个脚本。它正在工作,但我有一些文件我有问题。我是F#的新手,所以如何使用FileHelperException更改行以获得确切的行问题在哪里?谢谢 // Learn more about F# at http://fsharp.net // See the 'F# Tutorial' project for more help. open FileHelpers open System [<DelimitedRecord(",")>] type Cs

我从找到的一些资源中编写了这个脚本。它正在工作,但我有一些文件我有问题。我是F#的新手,所以如何使用FileHelperException更改行以获得确切的行问题在哪里?谢谢

// Learn more about F# at http://fsharp.net
// See the 'F# Tutorial' project for more help.
open FileHelpers  
open System

[<DelimitedRecord(",")>]
type CsvRecord =
    class
        val field1 : string
        val field2 : string
        val field3 : int
        new () = {
        field1 = ""
        field2 = ""
        field3 = 0
        }
    end

[<EntryPoint>]
let main argv = 
    use file = System.IO.File.CreateText("result.txt")
    let engine = new FileHelperEngine<CsvRecord>()
    engine.Encoding <- new Text.UTF8Encoding()
    let res = 
       try
          engine.ReadFile("test.csv")
       with
          | :? FileHelpersException -> Array.empty<CsvRecord>
    for record in res do
         fprintfn file "%s" record.field1
    printf "DONE!"
    let s = Console.ReadLine()
    0 // return an integer exit code
//了解更多关于F#at的信息http://fsharp.net
//有关更多帮助,请参阅“F#Tutorial”项目。
打开文件助手
开放系统
[]
CsvRecord型=
班
val字段1:字符串
val字段2:字符串
val字段3:int
新的()={
field1=“”
field2=“”
字段3=0
}
终止
[]
让主argv=
使用file=System.IO.file.CreateText(“result.txt”)
让引擎=新文件HelperEngine()
engine.Encoding Array.empty
记录在案
fprintfn文件“%s”记录。字段1
printf“完成!”
设s=Console.ReadLine()
0//返回整数退出代码
我建议您改用。当出现不匹配时,错误消息会说明发生错误的行

open FSharp.Data

[<EntryPoint>]
let main argv = 
    use file = System.IO.File.CreateText("result.txt")
    let csv = new CsvProvider<"test.csv">()
    for record in csv.Data do
        fprintfn file "%s" record.field1
打开FSharp.Data
[]
让主argv=
使用file=System.IO.file.CreateText(“result.txt”)
让csv=新CsvProvider()
用于csv中的记录。数据do
fprintfn文件“%s”记录。字段1
如果要忽略有错误的行,只需将
IgnoreErrors=true
作为额外参数传递给CsvProvider

我建议您改用。当出现不匹配时,错误消息会说明发生错误的行

open FSharp.Data

[<EntryPoint>]
let main argv = 
    use file = System.IO.File.CreateText("result.txt")
    let csv = new CsvProvider<"test.csv">()
    for record in csv.Data do
        fprintfn file "%s" record.field1
打开FSharp.Data
[]
让主argv=
使用file=System.IO.file.CreateText(“result.txt”)
让csv=新CsvProvider()
用于csv中的记录。数据do
fprintfn文件“%s”记录。字段1

如果要忽略有错误的行,只需将
IgnoreErrors=true
作为额外参数传递给CsvProvider

这个问题是关于您正在使用的FileHelpers库,而不是F#,因此查看。在这种情况下,您可以检查
ConvertException
而不是
FileHelpersException
,后者包含提供有关该成员的更多详细信息的成员

try
    engine.ReadFile("test.csv")
with
    | :? ConvertException as ex ->
        printfn "ERROR: Line %d Col %d" ex.LineNumber ex.ColumnNumber
        Array.empty<CsvRecord>
试试看
engine.ReadFile(“test.csv”)
具有
| :? ConvertException as ex->
printfn“错误:行%d列%d”例如LineNumber例如ColumnNumber
Array.empty

不过,我同意Gustavo的观点,您可能会发现使用CsvTypeProvider更容易。

这个问题是关于您正在使用的FileHelpers库,而不是F#,因此查看该库可能会有所帮助。在这种情况下,您可以检查
ConvertException
而不是
FileHelpersException
,后者包含提供有关该成员的更多详细信息的成员

try
    engine.ReadFile("test.csv")
with
    | :? ConvertException as ex ->
        printfn "ERROR: Line %d Col %d" ex.LineNumber ex.ColumnNumber
        Array.empty<CsvRecord>
试试看
engine.ReadFile(“test.csv”)
具有
| :? ConvertException as ex->
printfn“错误:行%d列%d”例如LineNumber例如ColumnNumber
Array.empty
不过,我同意Gustavo的观点,您可能会发现使用CsvTypeProvider更容易