I am currently working on a small PowerShell script that is supposed to split a CSV file with translations into individual files for the respective language. For this I use the Import-Csv
cmdlet to import the source file, which has about this format:
ResourceId;GermanTranslation;EnglishTranslation;Check
0;Deutscher Text;English text;OK
1; mit Leerzeichen ; with spaces ;OK
The goal is to get a line-by-line representation of the translations in the format ResourceId|EnglishTranslation|
. For this I have built the following script:
Set-Variable SOURCE_FILE -Option Constant -Value ".\sourceFile.csv"
Set-Variable RESULT_FILE -Option Constant -Value ".\resultFile.csv"
foreach ($row in (Import-Csv -Path $SOURCE_FILE -Delimiter ";")) {
Add-Content -Path $RESULT_FILE -Value ($row.RessourceId + "|" + $row.EnglishTranslation + "|")
}
Basically, everything works as desired, but when I examined the results, I noticed that the spaces with which some elements begin were no longer present in the result:
0|English text|
1|with spaces |
Unfortunately I didn't find a parameter in the MS documentation that addresses this problem, so I was unsure at first. After that, I took a look at RFC 4180, which describes the CSV file format more or less exactly. It states that spaces should be considered as part of a field and not ignored. Should means not must, so it may well be that there really is no option for this.
Is there a possibility to preserve the spaces without me having to parse the whole file on my own?