-1

Have a csv file in the following format: a,b

Goal is to store this csv file in a dictionary

Problem: csvOne has this as first field

kfjdfdsdsd, second value

aaaaaaa
sdasdasdaasdasdfffw

as a result it does not get stored in it's original format, i.e. only the below part gets stored:

key: "", value: kfjdfdsdsd

My code:

public void StoreInDictionary(string[] file, Dictionary<string, string> dictionary)
        {
            foreach (var line in file)
            {
                var cleansedLine = Regex.Replace(line, @"\s+", "");
                var commaIndex = cleansedLine.IndexOf(',');
                var valueOne = cleansedLine.Substring(0, commaIndex + 1);
                var valueTwo = cleansedLine.Substring(commaIndex + 1);

                if (!dictionary.ContainsKey(valueOne))
                {
                    dictionary.Add(valueOne, valueTwo);
                }
            }
        }

p.s. tried Replace \r\n too, did not work

Thanks a lot

Teallaair9
  • 39
  • 9
  • Have a look at this: http://stackoverflow.com/a/3508572/1845593 – user1845593 May 09 '17 at 21:31
  • @user1845593 tried this method too however got the same result – Teallaair9 May 09 '17 at 21:34
  • 2
    You say CSV but your sample with "kfjdfdsdsd" doesn't contain ,. I didn't understand your problem. Is the blank line or the fact that you don't have ,. And what you want to do in case there are multiple keys with the same text? – user1845593 May 09 '17 at 21:51
  • What is `csvOne` and why isn't it referenced in your code? – NetMage May 09 '17 at 23:38
  • @user1845593 Added value two to the csv file example for your interest. As for what I want to do in case there multiple keys with the same text, this should cover it: if (!dictionary.ConstainsKey(valueOne)) logic – Teallaair9 May 10 '17 at 06:15

2 Answers2

0

For output as:

     key               value
kfjdfdsdsd           second value
aaaaaaa              (blank)
sdasdasdaasdasdfffw  (blank)

use below code

public void StoreInDictionary(string[] file, Dictionary<string, string> dictionary)
{
    foreach (var line in file)
    {
        var cleansedLine = Regex.Replace(line, @"\s+", "");
        var commaIndex = cleansedLine.IndexOf(',');

        string valueOne = String.Empty;
        string valueTwo = String.Empty;

        if (commaIndex > 0)
        {
            valueOne = cleansedLine.Substring(0, commaIndex + 1);
            valueTwo = !String.IsNullOrWhiteSpace(cleansedLine.Substring(commaIndex + 1)) ? 
                        cleansedLine.Substring(commaIndex + 1) : 
                        String.Empty;
        }

        if (!dictionary.ContainsKey(valueOne))
        {
            dictionary.Add(valueOne, valueTwo);
        }
    }
}
Amey Kamat
  • 181
  • 2
  • 12
0

This should give you what you want. Just a quick note, you might have line with multiple "," my solution doesn't deal with that.

public void StoreInDictionary(string[] file, Dictionary<string, string> dictionary)
{
    foreach (var line in file)
    {
        if (!string.IsNullOrWhiteSpace(line))
        {
            string valueOne, valueTwo;

            var idx = line.IndexOf(',');

            if (idx >= 0)
            {
                valueOne = line.Substring(0, idx);
                valueTwo = line.Substring(idx + 1);
            }
            else
            {
                valueOne = line;
                valueTwo = string.Empty;
            }

            if (!dictionary.ContainsKey(valueOne))
            {
                dictionary.Add(valueOne, valueTwo);
            }
        }
    }
}
user1845593
  • 1,736
  • 3
  • 20
  • 37