Hello, help with parsing from a CSV file
I added several lines that need to be parsed, but I still have symbols that are not needed or, on the contrary, the symbols that are needed disappear, I don’t know very well how to use regex
Moon_Description,"""Moon description""","""Луна описание"""
Coin_Name,Coin,Монета
Coin_Description,"Coin description A = {0}, B = {2} C = {1}","Монета описание A = {0}, B = {2} C = {1}"
if csv adds " when downloading a file, then when parsing " which are at the end of the line remain, while other " disappear
“”“Moon description”“”
turns into
Moon description
and should turn into
“Moon description”
“”“Луна описание”“”
turns into
Луна описание"
and should turn into
“Луна описание”
also a line
“Монета описание A = {0}, B = {2} C = {1}”
turns into
Монета описание A = {0}, B = {2} C = {1}
and should turn into
Монета описание A = {0}, B = {2} C = {1}"
here is the code that parses
var languageValues = Enum.GetValues(typeof(ELanguage));
var csvParser = new Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");
var rows = csvRawData.Split(GetPlatformSpecificLineEnd());
var data = new Dictionary<ELanguage, Dictionary<string, string>>();
foreach (var lang in languageValues.Cast<ELanguage>())
data.Add(lang, new Dictionary<string, string>());
for (var rowId = 1; rowId < rows.Length; rowId++)
{
var cells = csvParser.Split(rows[rowId]);
var id = cells[0];
for(var cellId = 1; cellId < math.min(cells.Length, languageValues.Length + 1); cellId++)
{
var cell = cells[cellId];
cell = cell.Trim('"');
cell = cell.Replace("\"\"", "\"");
data[(ELanguage)cellId].Add(id, cell);
}
}
return data;