我希望能够过滤出CSV文件并对过滤后的数据执行数据验证。我想象for循环,但文件有200万个单元格,这需要很长时间。我正在使用Lumenworks CSVReader使用c#访问文件。
我找到了这个方法csvfile.Where<>但我不知道该在参数里放什么。对不起,我还是一个新的编码以及。
[EDIT]这是我加载文件的代码。谢谢你的帮助!
//Creating C# table from CSV data
var csvTable = new DataTable();
var csvReader = new CsvReader(newStreamReader(System.IO.File.OpenRead(filePath[0])), true);
csvTable.Load(csvReader);
//grabs header from the CSV data table
string[] headers = csvReader.GetFieldHeaders(); //this method gets the headers of the CSV file
string filteredData[] = csvReader.Where // this is where I would want to implement the where method, or some sort of way to filter the data
//I can access the rows and columns with this
csvTable.Rows[0][0]
csvTable.Columns[0][0]
//After filtering (maybe even multiple filters) I want to add up all the filtered data (assuming they are integers)
var dataToValidate = 0;
foreach var data in filteredData{
dataToValidate += data;
}
if (dataToValidate == 123)
//data is validated
我会阅读一些您正在使用的软件包的文档:
https://github.com/phatcher/CsvReader
https://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader
要明确回答过滤问题,因此它只包含您正在搜索的数据,请考虑以下内容:
var filteredData = new List<List<string>>();
using (CsvReader csv = new CsvReader(new StreamReader(System.IO.File.OpenRead(filePath[0])), true));
{
string searchTerm = "foo";
while (csv.ReadNextRecord())
{
var row = new List<string>();
for (int i = 0; i < csv.FieldCount; i++)
{
if (csv[i].Contains(searchTerm))
{
row.Add(csv[i]);
}
}
filteredData.Add(row);
}
}
这将给你一个字符串列表的列表,你可以枚举它来进行验证
int dataToValidate = 0;
foreach (var row in filteredData)
{
foreach (var data in row)
{
// do the thing
}
}
——旧答案——
如果没有看到你用来加载文件的代码,可能很难给你一个完整的答案,~2百万单元可能会很慢,不管什么。你的.Where
来自System.Linq
https://learn.microsoft.com/en us/dotnet/api/system.linq.enumerable.where?view=net - 6.0
一个使用.Where
//Read the file and return a list of strings that match the where clause
public List<string> ReadCSV()
{
List<string> data = File.ReadLines(@"C:UsersPublicDocumentstest.csv");
.Select(line => line.Split(','))
// token[x] where x is the column number, assumes ID is column 0
.Select(tokens => new CsvFileStructure { Id = tokens[0], Value = tokens[1] })
// Where filters based on whatever you are looking for in the CSV
.Where(csvFileStructure => csvFileStructure.Id == "1")
.ToList();
return data;
}
// Map of your data structure
public class CsvFileStructure
{
public long Id { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
根据此回答修改:
https://stackoverflow.com/a/10332737/7366061
没有csvreader。方法的地方。"where"是c#中Linq的一部分。下面的链接显示了使用Linq:
计算csv文件中的列的示例。https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/how-to-compute-column-values-in-a-csv-text-file-linq