Csv 从字符串和数组写入文件

Csv 从字符串和数组写入文件,csv,f#,Csv,F#,我正在尝试编写一个csv文件,其中一些值来自数组 let handle = "thetitle" let title = "The Title" let body = "ipsum lorem" let mutable variantPrice = [|1000,2000,3000,4000|] let mutable variantComparePrice = [|2000,4000,6000,8000|] let mutable storle

我正在尝试编写一个csv文件,其中一些值来自数组

    let handle = "thetitle"
    let title  = "The Title"
    let body = "ipsum lorem"
    let mutable variantPrice = [|1000,2000,3000,4000|]
    let mutable variantComparePrice = [|2000,4000,6000,8000|]
    let mutable storlek = ["50x50","50x60","50x70","50x80"]

    let Header = [|
                  (handle, title, body,variantPrice, variantComparePrice, storlek)
                 |]

    let lines = Header |> Array.map (fun (h, t, vp,vcp,b,s) -> sprintf "Handle\tTitle\tStorlek\tVariantPrice\tVariantComparePrice\tBody\n %s\t%s\t%s\t%s"h t s vp vcp b)

File.WriteAllLines( "data\\test.csv", lines, Encoding.UTF8) 
但问题是,行中的表达式应该是字符串,而我发送的却是字符串[]

Ideal would be that the csv file looked something like this

|handle|title|body|variantPrice|variantComparePrice|storlek|
|thetitle|The Title|ipsum lorem|1000|2000|50x50|
|thetitle|         |           |2000|4000|50x60|
|thetitle|         |           |3000|6000|50x70|
|thetitle|         |           |4000|8000|50x80|

第一个问题是,存储数据的变量(如
variantPrice
)当前仅包含一个元素(即元组)的数组-这是因为您使用
而不是
来分隔元素。最有可能的情况是,您需要以下内容:

let variantPrice = [|1000;2000;3000;4000|]
let variantComparePrice = [|2000;4000;6000;8000|]
let storlek = [|"50x50";"50x60";"50x70";"50x80"|]
这样,您就可以使用
Array.zip3
获得包含所有数据的单个数组(每行一项)

现在,您可以使用
Array.map
格式化各行。以下是我根据你的样本做出的猜测:

let lines = data |> Array.map (fun (vp, vcp, s) ->
  sprintf "|%s| | |%d|%d|%s" handle vp vcp s)
这是一个以字符串表示的行数组。最后,您可以将标题附加到行并将其写入文件:

let header = "|handle|title|body|variantPrice|variantComparePrice|storlek|"
System.IO.File.WriteAllLines("c:/temp/test.csv", 
  Array.append [| header |] lines, Encoding.UTF8) 

非常感谢。Array.zip3将参与此项目!
let header = "|handle|title|body|variantPrice|variantComparePrice|storlek|"
System.IO.File.WriteAllLines("c:/temp/test.csv", 
  Array.append [| header |] lines, Encoding.UTF8)