读取文件到列表
reading file to a list
我有一个这样的文本文件
a1,b1,30.04.2017
c1,d1,30.05.2017
我想将每一行添加到列表中,前两列将是字符串变量 s1 和 s2,第三列将转换为显示日期的变量
我一点经验都没有,通常我会在这里搜索我的答案。我只能找到包含变量名称和值的示例读取行,
我想我需要使用下面的代码
List<string> fileLines = new List<string>();
using (var reader = new StreamReader(fileName))
{
string line;
while ((line = r.ReadLine()) != null)
{
}
}
我不能做括号内的事情,即,
解析行,
将第一列分配给变量 s1
将第二列分配给变量 s2
将第三个转换为日期并将其分配给 d1
然后将行添加到列表
提前致谢
保持简单。
var text = File.ReadAllText(@"[FILEPATH]");
var fileLines = text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
但同样,这很容易用谷歌搜索。
以防万一,如果您需要完整的代码:这是我的版本
private static void TestReader()
{
var text = File.ReadAllText(@"D:\sample.txt");
var fileLines = new List<SampleData>();
foreach (var fileLine in text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
{
var lineData = fileLine.Split(',');
// Make sure all the keys are present / do a separate check
if (lineData.Length <= 2)
{
continue;
}
fileLines.Add(new SampleData
{
Code = Convert.ToString(lineData[0]),
Description = Convert.ToString(lineData[1]),
SelectedDate = lineData.Length >= 3 ? DateTime.ParseExact(lineData[2], "dd.MM.yyyy", null) : DateTime.MinValue
});
}
}
public class SampleData
{
public string Code { get; set; }
public string Description { get; set; }
public DateTime SelectedDate { get; set; }
}
在下面的代码中,我编写了解析后的数据,您可以随意使用它们:
List<string> fileLines = new List<string>();
using (var reader = new StreamReader(fileName))
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (line == "") continue; // refuse bare lines...
string a = line.Split(',')[0];
string b = line.Split(',')[1];
DateTime dt = DateTime.ParseExact(line.Split(',')[2], "dd.MM.yyyy", null);
fileLines.Add(line); // if you want....
}
}
使用 Linq :
var query = from line in File.ReadAllLines(filename)
where !string.IsNullOrEmpty(line)
let words = line.Split(";")
select new { Item1 = words[0], Item2 = words[1], Date = DateTime.ParseExact(words[2], "dd.MM.yyyy", null) };
我有一个这样的文本文件
a1,b1,30.04.2017
c1,d1,30.05.2017
我想将每一行添加到列表中,前两列将是字符串变量 s1 和 s2,第三列将转换为显示日期的变量
我一点经验都没有,通常我会在这里搜索我的答案。我只能找到包含变量名称和值的示例读取行,
我想我需要使用下面的代码
List<string> fileLines = new List<string>();
using (var reader = new StreamReader(fileName))
{
string line;
while ((line = r.ReadLine()) != null)
{
}
}
我不能做括号内的事情,即, 解析行, 将第一列分配给变量 s1 将第二列分配给变量 s2 将第三个转换为日期并将其分配给 d1 然后将行添加到列表
提前致谢
保持简单。
var text = File.ReadAllText(@"[FILEPATH]");
var fileLines = text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
但同样,这很容易用谷歌搜索。
以防万一,如果您需要完整的代码:这是我的版本
private static void TestReader()
{
var text = File.ReadAllText(@"D:\sample.txt");
var fileLines = new List<SampleData>();
foreach (var fileLine in text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
{
var lineData = fileLine.Split(',');
// Make sure all the keys are present / do a separate check
if (lineData.Length <= 2)
{
continue;
}
fileLines.Add(new SampleData
{
Code = Convert.ToString(lineData[0]),
Description = Convert.ToString(lineData[1]),
SelectedDate = lineData.Length >= 3 ? DateTime.ParseExact(lineData[2], "dd.MM.yyyy", null) : DateTime.MinValue
});
}
}
public class SampleData
{
public string Code { get; set; }
public string Description { get; set; }
public DateTime SelectedDate { get; set; }
}
在下面的代码中,我编写了解析后的数据,您可以随意使用它们:
List<string> fileLines = new List<string>();
using (var reader = new StreamReader(fileName))
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (line == "") continue; // refuse bare lines...
string a = line.Split(',')[0];
string b = line.Split(',')[1];
DateTime dt = DateTime.ParseExact(line.Split(',')[2], "dd.MM.yyyy", null);
fileLines.Add(line); // if you want....
}
}
使用 Linq :
var query = from line in File.ReadAllLines(filename)
where !string.IsNullOrEmpty(line)
let words = line.Split(";")
select new { Item1 = words[0], Item2 = words[1], Date = DateTime.ParseExact(words[2], "dd.MM.yyyy", null) };