简体   繁体   English

记录中的字符串列表以CSV格式显示?

[英]List of string in a record to CSV?

How does one write a list of string in a record to CSV without the lists being truncated? 如何将记录中的字符串列表写入CSV而不被截断?

CSV Writer: CSV编写器:

let toSepFile sep header (fileName:string) (s:'record seq)=

    let schemaType=typeof<'record>
    let fields = Reflection.FSharpType.GetRecordFields(schemaType) 

    let toStr fields = 
        fields
        |> Seq.fold(fun res field-> res+field+sep) ""

    use w = new System.IO.StreamWriter(fileName)

    if header then 
        let header_str= fields
                    |> Seq.map(fun field -> field.Name)
                    |> toStr

        w.WriteLine(header_str)

    let elemToStr (elem:'record) = 
        //for each field get value 
        fields
        |> Seq.map(fun field -> string (FSharpValue.GetRecordField(elem,field)))
        |> toStr

    s
    |>Seq.map(elemToStr)
    |>Seq.iter(fun elem -> w.WriteLine(elem))

Test Data (Deedle test set): 测试数据(Deedle测试集):

let peopleRecds = 
 [ { Name = "Joe"; Age = 51; Countries = [ "UK"; "US"; "UK"] }
 { Name = "Tomas"; Age = 28; Countries = [ "CZ"; "UK"; "US"; "CZ" ] }
 { Name = "Suzanne"; Age = 15; Countries = [ "US" ] } ]

Current CSV Output: 当前的CSV输出:

Name    Age Countries   
"Joe    51  [CZ; UK; US; ... ]  "
"Tomas  28  [CZ; UK; US; ... ]  "
"Suzanne    15  [US]    "

So is it possible see the full list of strings from the CSV output, instead of the "..."? 那么是否可以从CSV输出中看到字符串的完整列表,而不是“ ...”?

Edit: Desired output: 编辑:所需的输出:

    Name    Age Countries   
    "Joe      51    [CZ; UK; US]    "
    "Tomas    28    [CZ; UK; US; CZ]    "
    "Suzanne  15    [US]    "

The trouble you're having is that for List s, the ToString() method truncates the output. 您遇到的麻烦是对于ListToString()方法会截断输出。 The workaround is to not use ToString() , but instead use sprint "%A" *list here* . 解决方法是不使用ToString() ,而是使用sprint "%A" *list here*

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

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