C# 如何创建一个c字典,从.csv文件中读取键和值

C# 如何创建一个c字典,从.csv文件中读取键和值,c#,csv,dictionary,visual-studio-2015,C#,Csv,Dictionary,Visual Studio 2015,我有一个.csv文件,上面有缩写及其实际含义的列表,例如 大声笑,哈哈 我需要能够在文本框中搜索缩写,并用实际单词替换缩写。这是我迄今为止试图理解字典的内容,但无法解决如何从文件中读入值的问题 Dictionary<string, string> Abbreviations = new Dictionary<string, string>(); Abbreviations.Add("Laughing Out Loud", "lol"); foreach (KeyVa

我有一个.csv文件,上面有缩写及其实际含义的列表,例如

大声笑,哈哈

我需要能够在文本框中搜索缩写,并用实际单词替换缩写。这是我迄今为止试图理解字典的内容,但无法解决如何从文件中读入值的问题

 Dictionary<string, string> Abbreviations = new Dictionary<string, string>();
 Abbreviations.Add("Laughing Out Loud", "lol");
 foreach (KeyValuePair<string, string> abbrev in Abbreviations)
 {
     txtinput.Text = txtinput + "<<" + abbrev.Key + ">>";
 }

您可以先为文件创建一个流读取器,然后循环CSV中的所有值并将它们添加到字典中

static void Main(string[] args)
{
    var csv_reader = new StreamReader(File.OpenRead(@"your_file_path"));

    //declare your dictionary somewhere outside the loop.

    while (!csv_reader.EndOfStream)
    {
       //read the line and split if you need to with .split('')
        var line = reader.ReadLine();           

       //Add to the dictionary here

    }

   //Call another method for your search and replace. 
   SearchAndReplace(your_input)
}
然后实现该方法,搜索字典中是否存在输入,以及是否替换了输入

如果您更方便的话,可以使用LINQ将csv的值放入字典中

您可以尝试此LINQ解决方案GroupBy用于处理密钥多次出现在文件中的情况

  Dictionary<string, string[]> result =
     File.ReadLines("test.csv")
    .Select(line => line.Split(','))
    .GroupBy(arr => arr[0])
    .ToDictionary(gr => gr.Key,
                  gr => gr.Select(s => s[1]).ToArray());

我将假设您的输入文件在实际文本中可能有逗号,而不仅仅是分隔两个字段

现在,如果是这种情况,那么标准CSV文件格式将用于如下格式的文件:

Laughing Out Loud,LOL
"I Came, I Saw, I Conquered",ICISIC
但是,从您的示例来看,LOL前面有一个空格,因此您似乎没有使用标准CSV

因此,我将处理以下输入:

Laughing Out Loud, LOL
"I Came, I Saw, I Conquered",ICISIC
"to, too, or two", 2
because,B/C
对于此输入,此代码将生成一个字典:

var dictionary =
(
    from line in File.ReadAllLines("FILE.CSV")
    let lastComma = line.LastIndexOf(',')
    let abbreviation = line.Substring(lastComma + 1).Trim()
    let actualRaw = line.Substring(0, lastComma).Trim()
    let actual = actualRaw.StartsWith("\"") && actualRaw.EndsWith("\"")
        ? actualRaw.Substring(1, actualRaw.Length - 2)
        : actualRaw
    select new { abbreviation, actual }
).ToDictionary(x => x.abbreviation, x => x.actual);
不过你可以做得比这更好。很有可能创建一个超级函数,可以一次性完成所有替换

试试这个:

var translate =
(
    from line in File.ReadAllLines("FILE.CSV")
    let lastComma = line.LastIndexOf(',')
    let abbreviation = line.Substring(lastComma + 1).Trim()
    let actualRaw = line.Substring(0, lastComma).Trim()
    let actual = actualRaw.StartsWith("\"") && actualRaw.EndsWith("\"")
        ? actualRaw.Substring(1, actualRaw.Length - 2)
        : actualRaw
    select (Func<string, string>)(x => x.Replace(abbreviation, actual))
).Aggregate((f1, f2) => x => f2(f1(x)));
我得到这个结果:

我也是,一两个,因为我来了,我看见了,我征服了,大声大笑


你尝试了什么来读取文件中的值?然后我如何调用它并将其应用于文本框中输入的文本?请参阅我添加的示例。这与我最终使用的方法非常相似,谢谢你的帮助@史密斯-别担心。然后你应该投票并打勾作为答案。干杯
var translate =
(
    from line in File.ReadAllLines("FILE.CSV")
    let lastComma = line.LastIndexOf(',')
    let abbreviation = line.Substring(lastComma + 1).Trim()
    let actualRaw = line.Substring(0, lastComma).Trim()
    let actual = actualRaw.StartsWith("\"") && actualRaw.EndsWith("\"")
        ? actualRaw.Substring(1, actualRaw.Length - 2)
        : actualRaw
    select (Func<string, string>)(x => x.Replace(abbreviation, actual))
).Aggregate((f1, f2) => x => f2(f1(x)));
Console.WriteLine(translate("It was me 2 B/C ICISIC, LOL!"));