C# 将csv文件中的数据加载到键和值字典列表中
我有一个由文本列表组成的文件,如下所示: csv文件由3列组成。第一列的长度始终为5。所以我想循环遍历文件内容,将前5个字母存储为键,其余的列存储为值。我正在删除它们之间的逗号和子字符串,如下所示进行存储C# 将csv文件中的数据加载到键和值字典列表中,c#,file,dictionary,keyvaluepair,C#,File,Dictionary,Keyvaluepair,我有一个由文本列表组成的文件,如下所示: csv文件由3列组成。第一列的长度始终为5。所以我想循环遍历文件内容,将前5个字母存储为键,其余的列存储为值。我正在删除它们之间的逗号和子字符串,如下所示进行存储 static string line; static Dictionary<string, string> stations = new Dictionary<string, string>(); static void Main(string[] a
static string line;
static Dictionary<string, string> stations = new Dictionary<string, string>();
static void Main(string[] args)
{
// Dictionary<string, List<KeyValuePair<string, string>>> stations = new Dictionary<string, List<KeyValuePair<string, string>>>();
var lines = File.ReadAllLines(".\\ariba_sr_header_2017122816250.csv");
foreach (var l in lines)
{
line = l.Replace(",", "");
stations.Add(line.Substring(14),line.Substring(14, line.Length-14));
}
//read all key and value in file
foreach (KeyValuePair<string, string> item in stations)
{
Console.WriteLine(item.Key);
Console.WriteLine(item.Value);
}
Console.ReadLine();
}
静态字符串行;
静态字典站=新字典();
静态void Main(字符串[]参数)
{
//字典站=新字典();
变量行=File.ReadAllLines(“.\\ariba_sr_header_2017122816250.csv”);
foreach(行中的变量l)
{
行=l。替换(“,”,”);
添加(行。子字符串(14),行。子字符串(14,行。长度-14));
}
//读取文件中的所有键和值
foreach(站点中的KeyValuePair项)
{
控制台。写入线(项。键);
Console.WriteLine(项值);
}
Console.ReadLine();
}
调试后,输出为
我的预期结果如下:
我在这里看不到任何
KeyValuePair
。你有
00021,00014,Ordered
00021,00026,Ordered
00024,00036,Ordered
...
你想要什么
00021
00021
00024
000014Ordered
000026Ordered
000036Ordered
...
结果似乎是IEnumerable
。你可以试试Linq
在这里,我们将像0002100014,Ordered
这样的每一行拆分成单独的项
:{0002100014,Ordered}anf,然后在
SelectMany`的帮助下将它们合并回来。我们想要
00021
这是项[0]
000014已订购
其中是0
+项目[1]
+项目[2]
最后,我们希望先有短商品-OrderBy(item=>item.Length)
给你:
var stations = new Dictionary<string, string>();
var lines = File.ReadAllLines(@"C:\temp\22.txt");
foreach (var l in lines)
{
var lsplit = l.Split(',');
if (lsplit.Length > 1)
{
var newkey = lsplit[0];
var newval = lsplit[1] + lsplit[2];
stations[newkey] = newval;
}
}
//read all key and value in file
foreach (KeyValuePair<string, string> item in stations)
{
Console.WriteLine(item.Key + " = " + item.Value);
}
Console.ReadLine();
var stations=newdictionary();
var lines=File.ReadAllLines(@“C:\temp\22.txt”);
foreach(行中的变量l)
{
var lsplit=l.Split(',');
如果(lsplit.Length>1)
{
var newkey=lsplit[0];
var newval=lsplit[1]+lsplit[2];
站点[newkey]=newval;
}
}
//读取文件中的所有键和值
foreach(站点中的KeyValuePair项)
{
Console.WriteLine(item.Key+“=”+item.Value);
}
Console.ReadLine();
不完全是您所期望的输出,但希望它能有所帮助。子字符串(14)首先使用除14之外的所有内容。您希望子字符串(0,14)首先得到14。为什么是14??
var stations = new Dictionary<string, string>();
var lines = File.ReadAllLines(@"C:\temp\22.txt");
foreach (var l in lines)
{
var lsplit = l.Split(',');
if (lsplit.Length > 1)
{
var newkey = lsplit[0];
var newval = lsplit[1] + lsplit[2];
stations[newkey] = newval;
}
}
//read all key and value in file
foreach (KeyValuePair<string, string> item in stations)
{
Console.WriteLine(item.Key + " = " + item.Value);
}
Console.ReadLine();