Skip to content

Instantly share code, notes, and snippets.

@asabla
Last active February 16, 2023 19:59
Show Gist options
  • Save asabla/7f3c8b034a2752a9e7361e06f234d309 to your computer and use it in GitHub Desktop.
Save asabla/7f3c8b034a2752a9e7361e06f234d309 to your computer and use it in GitHub Desktop.
Transform CSV files with F# - Convert some columns to Capital first letters of each word
open FSharp.Data
open System.Globalization
[<Literal>]
let ResolutionFolder = __SOURCE_DIRECTORY__
// Schema can also be set to "" if you want it to auto generate
// column names similar to: Column1, Column2
type TargetCSV = CsvProvider<
"./test.csv",
";",
HasHeaders=false,
Schema="Id (int), SomeString (string), SomeDate (date option), SomeSecondString (string)",
ResolutionFolder=ResolutionFolder>
let loadedRows = new TargetCSV()
let cultureInfo = new CultureInfo("sv-SE", false)
let textInfo = cultureInfo.TextInfo
for row in loadedRows.Rows do
System.Console.WriteLine(textInfo.ToTitleCase(row.SomeString))
// Re-map results and manipulate both string columns with capitalizing
// first character in each word
let test = loadedRows.Map(fun row ->
TargetCSV.Row(
row.Id,
textInfo.ToTitleCase(row.SomeString),
row.SomeDate,
textInfo.ToTitleCase(textInfo.ToLower(row.SomeSecondString)),
row.Column5))
System.IO.File.WriteAllText("./result.csv", test.SaveToString())
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
1;some string;2022-02-02;SOME BIG STRING;123
2;some string second string;2022-03-03;SOME BIG SECOND STRING;321
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FSharp.Data" Version="6.0.1-beta002" />
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment