danny taki
danny taki

Reputation: 450

Reading only certain columns of a csv file with filehelpers

Trying to read only these columns of my csv file: Buyer Fullname, Ship to Address1, Ship to Address2, Ship to City, Ship to State, Ship to Zip, Ship to Country, Item Title, Quantity, Sale Price, Shipping and Handling.

This is my .CSV file:

Sales Record Number,User Id,Buyer Fullname,Buyer Phone Number,Buyer Email,Buyer Address 1,Buyer Address 2,Buyer City,Buyer State,Buyer Zip,Buyer Country,Item Number,Item Title,Custom Label,Quantity,Sale Price,Shipping and Handling,US Tax,Insurance,Cash on delivery fee,Total Price,Payment Method,Sale Date,Checkout Date,Paid on Date,Shipped on Date,Feedback left,Feedback received,Notes to yourself,PayPal Transaction ID,Shipping Service,Cash on delivery option,Transaction ID,Order ID,Variation Details,Global Shipping Program,Global Shipping Reference ID,Ship To Address 1,Ship To Address 2,Ship To City,Ship To State,Ship To Zip,Ship To Country

"911","trnkaso","TEDDY ROSCO","(815) 814-7454","[email protected]","6300 W Cherry St","","NILES","IL","60454-3406","United States","1115402028","SODIUM HYDROXIDE 50% in a one gallon poly bottle. 4 X 1 GALLON POLY BOTTLES","","2","$25.00","$0.00","$0.00","$0.00","","$100.00","PayPal","Sep-04-15","Sep-04-15","Sep-04-15","","No","","","0FG679030062A","UPS Ground","","1419197650001","","","No","","CHEERY ST","","NILES","IL","60714-3496","United States"
"912","siscokid8","MARK DWAYNE","(408) 943-1485","[email protected]","2050 Dam Ave","","San Jose","CA","95631-2104","United States","111113402518","LACQUER THINNER IN FIVE GALLON METAL PAIL","","1","$50.00","$10.00","$0.00","$0.00","","$153.00","PayPal","Sep-04-15","Sep-04-15","Sep-04-15","","No","","","23432J195640","UPS Ground","","1419241097001","","","No","","205065 Junction Ave","","San DIEGO","CA","95131-2104","United States"
"913","richmeltre","RICHIE FULLBRIGHT","(210) 863-36454","[email protected]","1323 Rosecolored Dr","","York","PA","17655-9185","United States","110829686817","Potassium Permanganate in a five lb container","","1","$35.00","$35.00","$0.00","$0.00","","$70.00","PayPal","Sep-06-15","Sep-06-15","Sep-06-15","","No","","","641682286830F","UPS Ground","","1419745125001","","","No","","ROSE GLASS DR","","York","PA","17244-9175","United States"

3, record(s) downloaded,from ,Sep-04-15,12:34:03, to ,Sep-06-15,04:10:47
Seller ID: [email protected]

Not sure how to skip over the fields I don't want and add only the fields I want. I guess I could create dummy fields to read in the csv file and then perform a remove on the those items afterwards, but is there a way to just not include them from the start? Also the last two lines will create an error as well I think, how do I handle them? Here's just the little bit of my code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using FileHelpers;

    namespace Ebay
    {
        class Program
        {
            static void Main()
            {
                var engine = new FileHelperEngine<Orders>();
                var records = engine.ReadFile("SalesHistory.csv");
            }  
        }
        [DelimitedRecord(",")]
        [IgnoreEmptyLines]
        class Orders
        {
            public string Name { get; set; }
            public string AddressLine1 { get; set; }
            public string AddressLine2 { get; set; }
            public string City { get; set; }
            public string State { get; set; }
            public string Title { get; set; }
            public string ItemPrice { get; set; }
            public string ShippingPrice { get; set; }
            public string Quantity { get; set; }
            public string PostalCode { get; set; }


        }
    }

Still not able to read the file here is how I changed my code:

namespace Ebay
{
    class Program
    {
        static void Main()
        {
            var engine = new FileHelperEngine<Orders>();
            var records = engine.ReadFile("SalesHistory.csv");
        }  
    }
    [DelimitedRecord(",")]
    [IgnoreEmptyLines]
    public class Orders
    {
        [FieldOrder(1)]
        private String DummyField1;

        [FieldOrder(2)]
        private String DummyField2;

        [FieldOrder(3)]
        public string Name { get; set; }

        [FieldOrder(4)]
        private String DummyField4;

        [FieldOrder(5)]
        private String DummyField5;

        [FieldOrder(6)]
        private String DummyField6;

        [FieldOrder(7)]
        private String DummyField7;

        [FieldOrder(8)]
        private String DummyField8;

        [FieldOrder(9)]
        private String DummyField9;

        [FieldOrder(10)]
        private String DummyField10;

        [FieldOrder(11)]
        private String DummyField11;

        [FieldOrder(12)]
        private String DummyField12;

        [FieldOrder(13)]
        public string Title { get; set; }

        [FieldOrder(14)]
        private String DummyField14;

        [FieldOrder(15)]
        public string Quantity { get; set; }

        [FieldOrder(16)]
        public string ItemPrice { get; set; }

        [FieldOrder(17)]
        public string ShippingPrice { get; set; }

       [FieldOrder(18)]
        private String DummyField18;

        [FieldOrder(19)]
        private String DummyField19;

        [FieldOrder(20)]
        private String DummyField20;

        [FieldOrder(21)]
        private String DummyField21;

        [FieldOrder(22)]
        private String DummyField22;

        [FieldOrder(23)]
        private String DummyField23;

        [FieldOrder(24)]
        private String DummyField24;

        [FieldOrder(25)]
        private String DummyField25;

        [FieldOrder(26)]
        private String DummyField26;

        [FieldOrder(27)]
        private String DummyField27;

        [FieldOrder(28)]
        private String DummyField28;

        [FieldOrder(29)]
        private String DummyField29;

        [FieldOrder(30)]
        private String DummyField30;

        [FieldOrder(31)]
        private String DummyField31;

        [FieldOrder(32)]
        private String DummyField32;

        [FieldOrder(33)]
        private String DummyField33;

        [FieldOrder(34)]
        private String DummyField34;

        [FieldOrder(35)]
        private String DummyField35;

        [FieldOrder(36)]
        private String DummyField36;

        [FieldOrder(37)]
        private String DummyField37;

        [FieldOrder(38)]
        public string AddressLine1 { get; set; }

        [FieldOrder(39)]
        public string AddressLine2 { get; set; }

        [FieldOrder(40)]
        public string City { get; set; }

        [FieldOrder(41)]
        public string State { get; set; }

        [FieldOrder(42)]
        public string PostalCode { get; set; }

        [FieldOrder(43)]
        public string Country { get; set; }

    }

Upvotes: 1

Views: 2352

Answers (2)

displayName
displayName

Reputation: 14379

I haven't used FileHelpers library. Never needed to. These manipulations are not difficult to do by myself. What I would do is as simple as 1-2-3:

  1. Read one line at a time;
  2. Split the line and get tokens;
  3. Take the only the tokens mentioned in the array of required fields.

The idea is to make addition of required fields a responsibility of Orders class instead of writing the logic for it in the Main().

In a code-pseudocode combination, it would look like the following:

In the Main method

public static void Main ()
{
    //Check the file path and other validations etc..

    using (var fileReader = new System.IO.StreamReader(@"C:\your\filepath\here"))
    {
        string line;
        while ((line = fileReader.ReadLine()) != null)
        {
            var tokens = line.Split(',');
            if (tokens.Length != ExpectedLength) continue; //this will filter the non-matching cases, including the last two lines
            myOrders.AddRequiredFields(tokens);
        }
    }
}

In the Orders class

The class Orders need to have the method which will only read the required tokens from all of the tokens that each line has. This will be:

//The properties like Name, Title, Quantity are already defined in this class

//Need to define an enum. Good programming practice

enum OrderFieldNumbers
{
    Buyer_Fullname = 0,
    Ship_to_Address1,
    Ship_to_Address2,
    ...,
    Name,
    ...,
    Title,
    ...  //Until all the fields are mentioned
};

public void AddRequiedFields(string[] tokens)
{
    //Simply add the ONLY THOSE FIELDS that you want to read.
    Name = tokens[OrderFieldNumbers.Name];
    Title = tokens[OrderFieldNumbers.Title];
    .
    .
    .
}

Every time you want to read specific fields, modify the AddRequiredFields according to your needs. You would have already enumerated all the fields of csv file in the OrderFieldNumbers property. Thus, you don't need to remember the positions of each field. You simply call the name as OrderFieldNumbers.myNeededColumnNumber and you get it.

Upvotes: 1

netniV
netniV

Reputation: 2418

You are almost there but you also need to add IgnoreFirst and IgnoreLast attributes I think. Otherwise, the last two or three lines will cause an error to be thrown as they don't have enough columns for the layout.

Upvotes: 1

Related Questions