Reputation: 2277
I'm trying to write an csv file where some of the values comes from arrays.
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)
But the problem is that the expression in lines are expected string but im sending in string[]
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|
Upvotes: 0
Views: 55
Reputation: 243051
The first issue is that your variables storing data like variantPrice
are currently arrays containing just a single element, which is a tuple - this is because you've separated elements using ,
rather than ;
. Most likely, you'll want something like:
let variantPrice = [|1000;2000;3000;4000|]
let variantComparePrice = [|2000;4000;6000;8000|]
let storlek = [|"50x50";"50x60";"50x70";"50x80"|]
With this, you can then use Array.zip3
to get a single array with all the data (one item per row).
let data = Array.zip3 variantPrice variantComparePrice storlek
Now you can use Array.map
to format the individual lines. The following is my guess based on your sample:
let lines = data |> Array.map (fun (vp, vcp, s) ->
sprintf "|%s| | |%d|%d|%s" handle vp vcp s)
This is an array of lines represented as strings. Finally, you can append the header to the lines and write this to a file:
let header = "|handle|title|body|variantPrice|variantComparePrice|storlek|"
System.IO.File.WriteAllLines("c:/temp/test.csv",
Array.append [| header |] lines, Encoding.UTF8)
Upvotes: 1