Skip to content

Instantly share code, notes, and snippets.

@ccdschool
Last active August 29, 2015 14:25
Show Gist options
  • Save ccdschool/c97d3c9f5501bf634618 to your computer and use it in GitHub Desktop.
Save ccdschool/c97d3c9f5501bf634618 to your computer and use it in GitHub Desktop.
CSV Viewer to be refactored
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// Sample data: https://www.census.gov/econ/cbp/download/index.htm
namespace CSVViewer
{
class Program
{
static void Main(string[] args)
{
var rawLines = File.ReadAllLines(args[0]);
var pageLen = 5;
if (args.Length > 1) pageLen = int.Parse(args[1]);
var pageLines = rawLines.Take(pageLen + 1);
var iFirstLineOfLastPage = 1;
while (true)
{
var records = pageLines.Select(l => Convert_line_to_record_fields(l, ","));
var colWidths = Enumerable.Range(0, records.First().Count())
.Select(i => records.Select(r => r[i].Length).Max())
.ToArray();
var headline = Create_disply_line_for_record(records.First(), colWidths);
var underlineRecord = Enumerable.Range(0, colWidths.Length).Select(i => new string('-', colWidths[i]));
var underline = string.Join("+", underlineRecord);
var displayLines = new[] {headline, underline}.Concat(records.Where((r,i) => i>0).Select(r => Create_disply_line_for_record(r, colWidths)));
Console.WriteLine(string.Join("\n", displayLines));
Console.Write("F(irst, L(ast, N(ext, P(rev, eX(it: ");
var cmd = char.ToLower(Console.ReadKey().KeyChar);
Console.WriteLine("\n");
switch (cmd)
{
case 'x':
return;
case 'f':
pageLines = rawLines.Take(pageLen + 1);
iFirstLineOfLastPage = 1;
break;
case 'l':
iFirstLineOfLastPage = rawLines.Length - (rawLines.Length - 1)%pageLen;
pageLines = new[] {rawLines[0]}.Concat(rawLines.Where((l,i) => i>0 && i>=iFirstLineOfLastPage));
break;
case 'n':
iFirstLineOfLastPage += pageLen;
if (iFirstLineOfLastPage >= rawLines.Length)
iFirstLineOfLastPage = rawLines.Length - (rawLines.Length - 1) % pageLen;
pageLines = new[] {rawLines[0]}.Concat(rawLines.Where((l, i) => i > 0 && i >= iFirstLineOfLastPage && i < (iFirstLineOfLastPage+pageLen)));
break;
case 'p':
iFirstLineOfLastPage -= pageLen;
if (iFirstLineOfLastPage < 1)
iFirstLineOfLastPage = 1;
pageLines = new[] {rawLines[0]}.Concat(rawLines.Where((l, i) => i > 0 && i >= iFirstLineOfLastPage && i < (iFirstLineOfLastPage+pageLen)));
break;
}
}
}
private static string[] Convert_line_to_record_fields(string line, string delimiter)
{
return Convert_line_to_record_fields(line, delimiter, new List<string>()).ToArray();
}
private static List<string> Convert_line_to_record_fields(string line, string delimiter, List<string> fields)
{
if (line == "") return fields;
if (line.StartsWith("\""))
{
line = line.Substring(1);
var iApo = line.IndexOf("\"");
fields.Add(line.Substring(0, iApo).Trim());
line = line.Substring(iApo + 1);
var iDelim = line.IndexOf(delimiter);
if (iDelim >= 0)
line = line.Substring(iDelim + 1);
else
line = "";
}
else
{
var iDelim = line.IndexOf(delimiter);
if (iDelim >= 0)
{
fields.Add(line.Substring(0, iDelim).Trim());
line = line.Substring(iDelim + 1);
}
else
{
fields.Add(line.Trim());
line = "";
}
}
return Convert_line_to_record_fields(line, delimiter, fields);
}
private static string Create_disply_line_for_record(string[] recordFields, int[] colWidths)
{
return string.Join("|", recordFields.Select((f, i) => f.PadRight(colWidths[i])));
}
}
}
zip name empflag emp_nf emp qp1_nf qp1 ap_nf ap est city stabbr cty_name
00501 HOLTSVILLE, NY A D 0 D 0 D 0 2 HOLTSVILLE NY SUFFOLK
01001 AGAWAM, MA G 8788 G 75093 G 322008 436 AGAWAM MA HAMPDEN
01002 AMHERST, MA G 15878 G 84225 G 360685 558 AMHERST MA HAMPSHIRE
01003 AMHERST, MA G 198 H 2054 H 8591 16 AMHERST MA HAMPSHIRE
01004 AMHERST, MA C S 0 H 1578 H 7063 19 AMHERST MA HAMPSHIRE
01005 BARRE, MA G 751 G 5778 G 24826 91 BARRE MA WORCESTER
01007 BELCHERTOWN, MA G 1749 G 10290 G 46766 217 BELCHERTOWN MA HAMPSHIRE
01008 BLANDFORD, MA H 321 H 712 G 2810 20 BLANDFORD MA HAMPDEN
01009 BONDSVILLE, MA H 119 G 1309 G 5161 14 BONDSVILLE MA HAMPDEN
01010 BRIMFIELD, MA G 363 G 2871 G 12272 79 BRIMFIELD MA HAMPDEN
01011 CHESTER, MA G 39 G 268 G 1234 16 CHESTER MA HAMPDEN
01012 CHESTERFIELD, MA G 80 G 437 G 2213 13 CHESTERFIELD MA HAMPSHIRE
01013 CHICOPEE, MA G 4788 G 41967 G 180975 323 CHICOPEE MA HAMPDEN
01014 CHICOPEE, MA A S 0 G 86 G 309 3 CHICOPEE MA HAMPDEN
01020 CHICOPEE, MA G 7375 G 58695 G 254591 529 CHICOPEE MA HAMPDEN
01021 CHICOPEE, MA G 190 G 1248 G 5988 7 CHICOPEE MA HAMPDEN
01022 CHICOPEE, MA G 2697 G 29380 G 118865 64 CHICOPEE MA HAMPDEN
01026 CUMMINGTON, MA H 123 H 834 H 4050 17 CUMMINGTON MA HAMPSHIRE
01027 EASTHAMPTON, MA G 4016 G 32228 G 136381 368 EASTHAMPTON MA HAMPSHIRE
01028 EAST LONGMEADOW, MA G 7617 G 75222 G 309052 504 EAST LONGMEADOW MA HAMPDEN
01029 EAST OTIS, MA H 135 H 793 H 4353 12 EAST OTIS MA BERKSHIRE
01030 FEEDING HILLS, MA G 966 G 5454 G 27750 183 FEEDING HILLS MA HAMPDEN
01031 GILBERTVILLE, MA G 72 H 328 G 1475 18 GILBERTVILLE MA WORCESTER
01032 GOSHEN, MA G 98 G 752 G 3392 22 GOSHEN MA HAMPSHIRE
01033 GRANBY, MA H 558 G 3665 G 18444 118 GRANBY MA HAMPSHIRE
01034 GRANVILLE, MA G 75 G 637 G 3481 29 GRANVILLE MA HAMPDEN
01035 HADLEY, MA G 4542 G 29564 G 127318 285 HADLEY MA HAMPSHIRE
01036 HAMPDEN, MA G 621 G 4206 G 25094 117 HAMPDEN MA HAMPDEN
01037 HARDWICK, MA C D 0 D 0 D 0 13 HARDWICK MA WORCESTER
01038 HATFIELD, MA G 1662 H 16863 H 71542 57 HATFIELD MA HAMPSHIRE
01039 HAYDENVILLE, MA H 252 H 2009 H 8324 35 HAYDENVILLE MA HAMPSHIRE
01040 HOLYOKE, MA G 16955 G 153350 G 625975 903 HOLYOKE MA HAMPDEN
01041 HOLYOKE, MA G 843 G 5443 G 23392 29 HOLYOKE MA HAMPDEN
01050 HUNTINGTON, MA G 151 G 778 G 3540 26 HUNTINGTON MA HAMPSHIRE
01053 LEEDS, MA H 1268 H 14705 H 56995 31 LEEDS MA HAMPSHIRE
01054 LEVERETT, MA G 106 G 735 G 3522 33 LEVERETT MA FRANKLIN
01056 LUDLOW, MA G 3776 G 33443 G 160007 403 LUDLOW MA HAMPDEN
01057 MONSON, MA G 823 G 7125 G 31719 148 MONSON MA HAMPDEN
01059 NORTH AMHERST, MA G 106 H 865 H 3795 13 NORTH AMHERST MA HAMPSHIRE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment