Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Dynamic;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Text.RegularExpressions;
- using CsvHelper;
- namespace AverageCsv
- {
- class Program
- {
- static void Main(string[] args)
- {
- var csvDir = args[0];
- var csvFiles = new DirectoryInfo(csvDir)
- .GetFiles()
- .Where(f => f.Extension.EndsWith("csv"))
- .Where(f => !f.Name.EndsWith("result.csv"));
- var dict = new Dictionary<string, dynamic>();
- var countRegex = new Regex(@"\w\w-(\d+).+");
- foreach (var csvFile in csvFiles)
- {
- Console.WriteLine("Full path: " + csvFile.FullName);
- var threadCount = countRegex.Match(csvFile.Name)
- .Groups[1].Value;
- using (var reader = new StreamReader(csvFile.FullName))
- using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
- {
- var records = csv.GetRecords<Record>().ToArray();
- var recordsFiltered = records;
- var a = recordsFiltered.Select(x => x.Label + ": " + x.Average).ToArray();
- Console.WriteLine(string.Join("\n", a));
- var firstAverage = records.First();
- foreach (var rec in recordsFiltered)
- {
- if (!dict.ContainsKey(rec.Label))
- {
- dynamic exp = new ExpandoObject();
- exp.Label = rec.Label;
- exp.u10 = 0;
- exp.u50 = 0;
- exp.u100 = 0;
- exp.u150 = 0;
- exp.u200 = 0;
- exp.u300 = 0;
- dict.Add(rec.Label, exp);
- }
- var e = dict[rec.Label] as IDictionary<String, Object>;
- e["u" + threadCount] = rec.Average;
- }
- }
- }
- using (var writer = new StreamWriter(csvDir + "/result.csv"))
- using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
- {
- var finalRecords = dict.Values.ToList();
- csv.WriteRecords(finalRecords);
- }
- }
- }
- class Record
- {
- public string Label { get; set; }
- public int Average { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement