public void FastCsv_FeatureSupport_WikipediaExample1() { string toTest = @"Year,Make,Model,Extra 1997,Ford,E350,""2,35"" ""1997"",""Ford"",""E350"",""quoted"" 1997,Ford,E350,""Super, luxurious truck"" 1997,Ford,E350,""Super, """"luxurious"""" truck"" 1997,Ford,E350,"" Super, luxurious truck "" 2000,Mercury,Cougar,""2,38"" "; string[][] result = new string[][] { new string[] { "Year", "Make", "Model", "Extra" }, new string[] { "1997", "Ford", "E350", "2,35" }, new string[] { "1997", "Ford", "E350", "quoted" }, new string[] { "1997", "Ford", "E350", "Super, luxurious truck" }, new string[] { "1997", "Ford", "E350", "Super, \"luxurious\" truck" }, new string[] { "1997", "Ford", "E350", " Super, luxurious truck " }, // space new string[] { "2000", "Mercury", "Cougar", "2,38" }, }; using var str = new MemoryStream(Encoding.ASCII.GetBytes(toTest)); using var sr = new StreamReader(str); var lines = FastCsv.ReadDelimiter(sr, ',').ToArray(); Assert.Equal(result, lines); }
public void FastCsv_FeatureSupport_WikipediaExample2() { // must force \r\n to test in multiple enviroments string toTest = @"Year,Make,Model,Description,Price 1997,Ford,E350,""ac, abs, moon"",3000.00 1999,Chevy,""Venture """"Extended Edition"""""","""",4900.00 1999,Chevy,""Venture """"Extended Edition, Very Large"""""",,5000.00 1996,Jeep,Grand Cherokee,""MUST SELL!" + "\r\n" + @"air, moon roof, loaded"",4799.00 "; string[][] result = new string[][] { new string[] { "Year", "Make", "Model", "Description", "Price" }, new string[] { "1997", "Ford", "E350", "ac, abs, moon", "3000.00" }, new string[] { "1999", "Chevy", "Venture \"Extended Edition\"", "", "4900.00" }, new string[] { "1999", "Chevy", "Venture \"Extended Edition, Very Large\"", "", "5000.00" }, new string[] { "1996", "Jeep", "Grand Cherokee", "MUST SELL!\r\nair, moon roof, loaded", "4799.00" }, }; using var str = new MemoryStream(Encoding.ASCII.GetBytes(toTest)); using var sr = new StreamReader(str); var lines = FastCsv.ReadDelimiter(sr, ',').ToArray(); Assert.Equal(result, lines); }
private static void processaCsvParaTxt() { string[] meses = { "Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro" }; FastCsv csv = new FastCsv(); csv.Encoding = Encoding.UTF7; var dados = csv.ReadDelimiter(File.OpenRead("teste.csv")) .Skip(1) .Select(l => new { Ano = l[0].ToInt(), Mes = l[1].ToInt(), Parlamentar = l[3], Valor = l[5].Trim().Replace("R$", "R$ "), Cnpj = l[6], Empresa = l[7], }) .GroupBy(o => o.Parlamentar) .Select(o => new { Key = o.Key, Dados = o.ToArray() }); using var file = File.Open("dados.txt", FileMode.Create, FileAccess.Write); using var sw = new StreamWriter(file); foreach (var v in dados) { sw.WriteLine($"{v.Key}"); Console.WriteLine(v.Key); var anos = v.Dados.GroupBy(o => o.Ano) .Select(g => new { Ano = g.Key, notas = g.OrderBy(o => o.Mes) }) .OrderBy(o => o.Ano); foreach (var ano in anos) { //sw.WriteLine($"Em {ano.Ano}:"); foreach (var nota in ano.notas) { sw.WriteLine($"{meses[nota.Mes - 1]}/{nota.Ano}: Nota no valor de {nota.Valor} pago à empresa {nota.Empresa}, CNPJ {nota.Cnpj}."); } sw.WriteLine(); } sw.WriteLine(); } }
public void FastCsv_Garbage() { string toTest = "NO_CAND\";\"DS_CARGO\";\"CD_CARGO\";\"NR_CAND"; string[] result = { "NO_CAND", "DS_CARGO", "CD_CARGO", "NR_CAND" }; using var str = new MemoryStream(Encoding.ASCII.GetBytes(toTest)); using var sr = new StreamReader(str); var lines = FastCsv.ReadDelimiter(sr).ToArray(); Assert.Equal(result, lines[0]); }
public static void run() { List <VeiculoModel> lst = new List <VeiculoModel>(); FastCsv parser = new FastCsv() { Encoding = Encoding.UTF7, SupportQuotedLineBreaks = true, }; // ler cada arquivo foreach (var arq in Directory.GetFiles(@"N:\Organizando\Dados Crawlers\Senado\Abastecimentos", "*.csv")) { var fi = new FileInfo(arq); var nameParts = fi.Name[..^ 4].Split('_');
public void FastCsv_SemicolonWithComma() { string toTest = @"Year;Make;Model;Length 1997;Ford;E350;2,35 2000;Mercury;Cougar;2,38 "; string[][] result = new string[][] { new string[] { "Year", "Make", "Model", "Length" }, new string[] { "1997", "Ford", "E350", "2,35" }, new string[] { "2000", "Mercury", "Cougar", "2,38" }, }; using var str = new MemoryStream(Encoding.ASCII.GetBytes(toTest)); using var sr = new StreamReader(str); var lines = FastCsv.ReadDelimiter(sr).ToArray(); Assert.Equal(result, lines); }
public void FastCsv_MultiLineGarbage() { string toTest = @"NO_CAND"";""DS_CARGO"";""CD_CARGO"";""NR_CAND 1234"";""A32"";""10"";""9999"; string[][] result = new string[][] { new string[] { "NO_CAND", "DS_CARGO", "CD_CARGO", "NR_CAND" }, new string[] { "1234", "A32", "10", "9999" } }; var ms = new MemoryStream(Encoding.ASCII.GetBytes(toTest)); FastCsv fast = new FastCsv() { SupportQuotedLineBreaks = false, }; var lines = fast.ReadDelimiter(ms).ToArray(); Assert.Equal(result, lines); }
public void FastCsv_BaseTest() { string toTest = @"Year,Make,Model 1997,Ford,E350 2000,Mercury,Cougar 1997,Ford,""E350"""; string[][] result = new string[][] { new string[] { "Year", "Make", "Model" }, new string[] { "1997", "Ford", "E350" }, new string[] { "2000", "Mercury", "Cougar" }, new string[] { "1997", "Ford", "E350" }, }; using var str = new MemoryStream(Encoding.ASCII.GetBytes(toTest)); using var sr = new StreamReader(str); var lines = FastCsv.ReadDelimiter(sr, ',').ToArray(); Assert.Equal(result, lines); }