我需要从 csv 获取数据到字典,但是当我尝试编译此代码时,我收到错误“已添加具有相同键的项目”。怎么做 ?`
Dictionary<string, string> dic = new Dictionary<string, string>();
public void AddToDic()
{
string line = "";
using (StreamReader sr = new StreamReader(@"words.txt"))
{
while (sr.Peek() != -1)
{
line = line + sr.ReadLine();
string[] splitted = line.Split(' ');
dic.Add(splitted[0], splitted[1]); //ERROR An item with the same key has already been added.
}
}
}
//text in words.txt is like: "car auto" newline "water voda" etc...