【发布时间】:2012-03-20 16:57:11
【问题描述】:
我使用streamreader 读取.csv 文件,然后我需要拆分值并将它们放入字典中。到目前为止我有:
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
Dictionary<string, string> dict = new Dictionary<string, string>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
using (StreamReader reader = new StreamReader("textwords0.csv"))
{
string line;
while ((line = reader.ReadLine()) != null)
{
string[] parts = line.Split(',');
dict.Add(parts[0], parts[1]);
}
}
}
我不断收到错误消息“无法从 'string[]' 转换为 'string'”,但不知道如何解决它。
提前致谢!
更新: ...我不小心打开了 csv 文件并且它现在可以工作了,很抱歉浪费了时间,伙计们认为我打开了一个不同的电子表格,一些非常有用的建议虽然感谢所有的帮助!
【问题讨论】:
-
哪一行给你错误? dict.Add 行?
-
我们可以查看源代码吗? (textwords0.csv)?
-
VS 说的是第 33 行,也就是最后一个 }。
-
不确定你是否解决了这个问题,但你确定你得到了你在问题中写的异常吗?我只是在一些随机数据上运行它,它工作得很好
-
使用CsvParser,有相当不错的第 3 方可以处理您甚至没有想到的边缘情况。
标签: c# dictionary streamreader