private static void FromFile(Stream stream) { List <int[]> values = new List <int[]>(); using (TextReader streamReader = new StreamReader(stream)) { using (var reader = new CsvHelper.CsvParser(streamReader)) { for (var record = reader.Read(); record != null; record = reader.Read()) { values.Add(record.Select(int.Parse).ToArray()); } int[,] matrix = new int[values.Count(), values.First().Count()]; for (var r = 0; r < matrix.GetLength(0); r++) { for (var c = 0; c < matrix.GetLength(1); c++) { matrix[r, c] = values[r][c]; } } OnImport?.Invoke(null, new CsvImport(matrix)); } } }
internal static IList <IDictionary <string, string> > FetchDictionary(string text) { using (var sr = new StringReader(text)) using (var parser = new CsvHelper.CsvParser(sr)) { parser.Configuration.Delimiter = "\t"; var list = new List <IDictionary <string, string> >(); string[] headers = parser.Read(); for (;;) { var dict = new Dictionary <string, string>(); var data = parser.Read(); if (data == null || data.Length < 1) { break; } for (int i = 0; i < headers.Length; i++) { dict[headers[i]] = data[i]; } list.Add(dict); } return(list); } }
internal OrderDetailsReader(string sourceFile) { if (!File.Exists(sourceFile)) { throw new FileNotFoundException($"Source file {sourceFile} does not exist."); } m_Reader = new System.IO.StreamReader(sourceFile); m_parser = new CsvHelper.CsvParser(m_Reader); }
public void Dispose() { if (m_parser != null) { m_parser.Dispose(); m_parser = null; } if (m_Reader != null) { m_Reader.Dispose(); m_Reader = null; } }
private string GenerateCsvConfigText(string targetFilePath, Encoding targetFileEncoding, CultureInfo cultureInfo, bool hasHeaders, bool autoTypeDetection) { using (var reader = new StreamReader(targetFilePath, targetFileEncoding)) { var parser = new CsvHelper.CsvParser(reader, cultureInfo); string[] headers = null; if (hasHeaders) { headers = parser.Read(); } var row = parser.Read(); var config = new T4.ConfigurationTemplateGenerator(targetFileEncoding.WebName, cultureInfo, autoTypeDetection, row, headers); return(config.TransformText()); } }
private static IDictionary <string, string> ReadQuestions(string csvQuestions) { var result = new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase); using (var fs = File.OpenRead(csvQuestions)) using (var sw = new StreamReader(fs)) using (var csv = new CsvHelper.CsvParser(sw)) { var row = csv.Read(); while (row != null) { result.Add(row[0].ToLower(), row[1]); row = csv.Read(); } } return(result); }
public void CsvHelperCsv() { var tr = TestData.GetTextReader(); var config = new CsvHelper.Configuration.CsvConfiguration(CultureInfo.CurrentCulture) { BufferSize = BufferSize }; var r = new CsvHelper.CsvParser(tr, config); while (r.Read()) { for (int i = 0; i < r.Count; i++) { var s = r[i]; } } }
public static void VerifyCSVFile(String csvFile, string delimiter, int totalRows, int?colCount, Boolean?quoteall) { Assert.IsTrue(File.Exists(csvFile)); int count = 0; using (var reader = new StreamReader(csvFile)) using (var parser = new CsvHelper.CsvParser(reader, System.Globalization.CultureInfo.InvariantCulture)) { parser.Configuration.Delimiter = delimiter; while (true) { var records = parser.Read(); if (records == null) { break; } count++; if (colCount.HasValue) { Assert.AreEqual(colCount.Value, records.Length); } if (quoteall.HasValue) { if (quoteall.Value) { } else { } } else { } } } Assert.AreEqual(totalRows, count); }
/// <summary> /// /// </summary> /// <param name="path"></param> /// <returns></returns> private IEnumerable <string[]> ReadInternal(string path) { using (var reader = new System.IO.StreamReader(path)) { var configuration = new CsvHelper.Configuration.CsvConfiguration(); configuration.Delimiter = CultureInfo.CurrentCulture.TextInfo.ListSeparator; configuration.CultureInfo = CultureInfo.CurrentCulture; configuration.AllowComments = true; configuration.Comment = '#'; using (var parser = new CsvHelper.CsvParser(reader, configuration)) { while (true) { var fields = parser.Read(); if (fields == null) { break; } yield return(fields); } } } }
public TestMath_NetPage() { InitializeComponent(); // Test embeddedResource var rootFolder = FileSystem.Current.LocalStorage; var file = ResourceLoader.GetEmbeddedResourceStream(Assembly.Load(new AssemblyName(assmName)), csvFile); Debug.WriteLine(file.Length); // Xamarin default solution var assem = typeof(TestMath_NetPage).GetTypeInfo().Assembly; Stream stream = assem.GetManifestResourceStream("TestMath.Net.Logs.csv"); // Test CSV Helper //var reader = new System.IO.StreamReader(stream); var reader = new System.IO.StreamReader(file); var csv = new CsvHelper.CsvParser(reader); Debug.WriteLine(csv.Read()); //Test CSV reading all records var csvAll = new CsvHelper.CsvReader(reader); csvAll.Configuration.HasHeaderRecord = false; //csvAll.Read(); //var testField = csvAll.GetField<double>(0); var records = csvAll.GetRecords <double>().ToList(); //Debug.WriteLine(csv.FieldCount); Debug.WriteLine(records.Count()); //Matrix<float> csvMatrix = Test.DelimitedReader.ReadStream<float>(stream, false, ","); //Debug.WriteLine("csvMatrix", csvMatrix); // Test create Math.NET matrix with CSV Helper // Test Math.NET matrix // Generate a dense matrix with 500 rows and 500 column // FIlled with random numbers var m = Matrix <double> .Build.Random(500, 500); var v = Vector <double> .Build.Random(500); var y = m.Solve(v); Debug.WriteLine(y.Count); // Shorter version var M = Matrix <double> .Build; var V = Vector <double> .Build; var x = M.Random(500, 500); var z = V.Random(500); }