2

I tried to import a CSV file containing 2 columns (e.g. Date and Price) and print it on the console. But on my console, numbers are not ordered by columns. They are one next to another.

First Question: how can I print it on the console correctly?

Second Question: can I combine these two arrays, create a matrix and print the matrix on the console? Is it easier?

Here is my code:

using system.IO;
var reader=new StreamReader(File.OpenRead(@"filename.csv");
list<string> listA=new list<string>();
list<string> listB=new list<string>();
while (!Reader.EndofStream)
{ 
     var Line=reader.ReadLine();
     var valuesline.split(';');  
     listA.Add(values[0]);
     listB.Add(values[1]);
}
console.WriteLine("Column1:"); 
foreah (var Element in listA)
{
    console.write(Element);
    console.WriteLine("Column2:"); 
}   
foreah (var Element in listB); 
{
    console.write(Element);
    string[] S= listA.toArray;
    string[] o=listB.toArray; 
}
8
  • possible duplicate of how to read data from csv file into C# console Application Commented Jul 11, 2015 at 15:32
  • foreach (var Element in listA); a semi colon after the parenthesis will end the statement (it valid to do that) and not loop to the next line(s). Surely you mean it without the semi colon? foreach (var Element in listA) Commented Jul 11, 2015 at 15:33
  • What is your desired output? Can you show us what a line of output you want would look like. I.E. Row 1 = [1,2] (new line) Row 2 = [2,3] etc? Commented Jul 11, 2015 at 15:49
  • My desired output is to print these two columns Date and Price on the console like this Commented Jul 11, 2015 at 21:35
  • Example Date Price Commented Jul 11, 2015 at 21:37

2 Answers 2

1

This sounds like a formatting question.

Console.WriteLine borrows off of string.Format to allow for text specification. Format allows one to position items and provide padding. For what you want is a column to specifically be padded.

Here is an example of three columns on a line, note the numbers 0, 1, 2 which indexes into the data following the specification. Next to that index value is a following number -15 which allows for a padding of 15 spaces for the data to be fit into on each of the columns:

Console.WriteLine("{0,-15} {1,-15} {2,-15}", "Alpha", "Beta", 12);
Console.WriteLine("{0,-15} {1,-15} {2,-15}", "More Text", "B", 122000);

Output:

Alpha           Beta            12             
More Text       B               122000 

See String Formatting for more information.

Sign up to request clarification or add additional context in comments.

Comments

0

Without knowing what your output should look like I'd create a class to store my results and then override that classes ToString function. This way you can just to string your results.

Note: I added two ways to do the ToString to show you that you can work with this a little to get the format you'd like on your output. If you need tabs or whatnot you can add that into the string format.

EDIT: I changed CsvRow to strings so they can take your Csv values. And I changed the ToString to more line up with your needs. Just take the class instantiate it from your CSV and print it to console. using System; using System.Collections.Generic;

public class CsvRow
{
    public CsvRow()
    {
    }

    public CsvRow(string a, string b)
    {
        A = a;
        B = b;
    }
    public string A {get; set;}
    public string B {get; set;}

    public string ToString(int rowNumber)
    {
        return String.Format("Row{0}: {1} {2}", rowNumber, A, B);
    }

}

public class Program
{
    public static void Main()
    {
        var results = new List<CsvRow>();
        results.Add(new CsvRow(DateTime.Now.ToString(), "200"));
        results.Add(new CsvRow(DateTime.Now.ToString(), "300"));            

        var i = 0;
        foreach (var result in results)
        {
            Console.WriteLine(result.ToString(i));
            i++;
        }       
    }
}

Link to DotFiddle I used

4 Comments

Thanks for your code. My desired output is to print the result of a cvs file row by row on the C# console. The output looks like this
Example: Date Price
Row1: 01/01/2015 200
Row2: 01/01/2014 100

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.