我们如何才能只得到CSV标题,其中有特定列中的数据?

628mspwn  于 2023-09-27  发布在  其他
关注(0)|答案(3)|浏览(77)

| 列A|列B|列C|列D|
| --|--|--|--|
| ABC|--|BCD|--|
| EDF| SDG|--|rzh|
例如:在行1中,数据在B列和D列中不可用。因此,对于行1,标题输出应该是列A、列C。第2行相同,C列数据不可用。因此,对于第2行,标题输出应为列A、列B、列D。

string csvFilePath = "your_csv_file.csv";

        using (var reader = new StreamReader(csvFilePath))
        using (var csv = new CsvReader(reader, new CsvConfiguration(CultureInfo.InvariantCulture)))
        {
            // Read the CSV records into a dynamic list
            var records = csv.GetRecords<dynamic>().ToList();

            // Get the headers from the first record
            var headers = ((IDictionary<string, object>)records[0]).Keys.ToList();

            // Create a list to store headers with data
            var headersWithData = new List<string>();

            // Iterate through headers and check if any row in the column has data
            foreach (var header in headers)
            {
                if (records.Skip(1).Any(record => !string.IsNullOrWhiteSpace(record[header]?.ToString())))
                {
                    headersWithData.Add(header);
                }
            }

            // Print the filtered column headers with data
            Console.WriteLine("Column Headers with Data:");
            foreach (var header in headersWithData)
            {
                Console.WriteLine(header);
            }
        }
exdqitrt

exdqitrt1#

就像这样简单:

string[][] results =
    File
        .ReadAllLines(csvFilePath)
        .Select(x => x.ParseCsvLine())
        .Select(x => x.Where(y => !String.IsNullOrEmpty(y)).ToArray())
        .ToArray();

ParseCsvLinestring从CSV转换为stringp[]

wmomyfyw

wmomyfyw2#

看起来你把dynamic数据类型当作IDictionary数据类型。数据的类型转换将解决您的问题。

// Iterate through headers and check if any row in the column has data
foreach (var header in headers)
{
    if (records.Skip(1).Any(record => !string.IsNullOrWhiteSpace(((IDictionary<string, object>)record)[header]?.ToString())))
    {
        headersWithData.Add(header);
    }
}
ffx8fchx

ffx8fchx3#

你的想法是对的循环遍历记录,然后将dynamic记录转换为IDictionary<string, object>。使用LINQ查找列值不是string.IsNullOrWhiteSpace的项,然后选择Key,即列标题。使用string.Join连接返回的IEnumerable<string>头名称,这些头名称包含该行的数据。

using (var reader = new StringReader("Column A,Column B,Column C,Column D\nabc,,bcd,\nedf,sdg,,rzh"))
using (var csv = new CsvReader(reader, new CsvConfiguration(CultureInfo.InvariantCulture)))
{
    // Read the CSV records into a dynamic list
    var records = csv.GetRecords<dynamic>().ToList();

    // Create a list to store headers with data
    var headersWithData = new List<string>();

    // Iterate through each record and check if a column has data   
    foreach (var record in records)
    {
        var rowHeaders = ((IDictionary<string, object>)record)
            .Where(item => !string.IsNullOrWhiteSpace(item.Value?.ToString()))
            .Select(item => item.Key);
            
        headersWithData.Add(string.Join(",", rowHeaders));      
    }
    
    // Print the filtered column headers with data
    Console.WriteLine("Column Headers with Data:");
    foreach (var header in headersWithData)
    {
        Console.WriteLine(header);
    }
}

相关问题