Aggregating different file CSV

这一生的挚爱 提交于 2019-12-14 03:36:07

问题


I'm beginner in C# and I don't know the API in details. I would like to write a one .csv that contains a single day from each of those files, and contains the data that was there in each file.


回答1:


You have to use plain loops in C#3.0, you could fill a Dictionary for example:

string dir = @"C:\DirectoryName";
string[] files = Directory.GetFiles(dir, "*.csv", SearchOption.TopDirectoryOnly);
var dateFiles = new Dictionary<DateTime, List<string>>();

foreach (string file in files)
{
    string fn = Path.GetFileNameWithoutExtension(file);
    if (fn.Length < "yyyyMMdd_HHmmss".Length)
        continue;
    string datePart = fn.Remove("yyyyMMdd".Length); // we need only date
    DateTime date;
    if (DateTime.TryParseExact(datePart, "yyyyMMdd", DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out date))
    {
        bool containsDate = dateFiles.ContainsKey(date);
        if (!containsDate) dateFiles.Add(date, new List<string>());
        dateFiles[date].Add(file);
    }
}

foreach(KeyValuePair<DateTime, List<string>> dateFile in dateFiles)
    MergeFilesForDay(dir, dateFile.Key, dateFile.Value);

and here's a method that creates the new files:

static void MergeFilesForDay(string dir, DateTime date, List<string> files)
{ 
    string file = Path.Combine(dir, date.ToString("yyyyMMdd") + ".csv");
    using(var stream = File.CreateText(file))
    {
        foreach(string fn in files)
            foreach(string line in File.ReadAllLines(fn))
                stream.WriteLine(line);
    }
}



回答2:


This will return all CSV filenames grouped by the first 8 characters of their filename, which is the date those files belong to.

public Dictionary<string, List<string>> GetCsvFilesGroupedByDate(string csvDirectory)
{
    var csvFiles = Directory.GetFiles(csvDirectory, "*.csv");

    var groupedByDate = csvFiles.GroupBy(s => Path.GetFileName(s).Substring(0, 8));

    return groupedByDate.ToDictionary(g => g.Key, g => g.ToList());
}

You can then loop over the results:

var files = GetCsvFilesGroupedByDate(@"C:\CSV\");

foreach (var filesPerDate in files)
{
    // parse / concatenate CSV using filesPerDate.Key and filesPerDate.ToList()
}


来源:https://stackoverflow.com/questions/26361974/aggregating-different-file-csv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!