jes*_*ice 9 c# dictionary streamreader
我已经使用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]);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不断收到错误"无法从'string []'转换为'string'",但无法解决如何修复它.
提前致谢!
更新:...我不小心将csv文件保持打开状态并且现在正在工作,抱歉浪费时间我们认为我有一个不同的电子表格打开,一些非常有用的建议,但感谢所有的帮助!
Tay*_*ick 30
如果您正在使用.NET 4.0,以下内容非常简洁,应该完成同样的事情:
var dict = File.ReadLines("textwords0.csv").Select(line => line.Split(',')).ToDictionary(line => line[0], line => line[1]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17675 次 |
| 最近记录: |