简体   繁体   English

使用FileHelpers仅读取CSV文件的某些列

[英]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 . 尝试仅读取我的csv文件的这些列: Buyer Fullname ,运Ship to Address1 ,运Ship to Address2 ,运Ship to City ,运Ship to State ,运Ship to Zip ,运Ship to CountryItem TitleQuantitySale PriceShipping and Handling

This is my .CSV file: 这是我的.CSV文件:

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","trnadfo21@yahoo.com","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","rasdfdsaay@siscobreakers.com","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","rcdasfasdftrevino@treasdfavino6.com","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: non@non.com

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? 我想我可以创建虚拟字段以读取csv文件,然后在之后对这些项目执行删除,但是有没有办法从一开始就不包括它们? 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; }

    }

You are almost there but you also need to add IgnoreFirst and IgnoreLast attributes I think. 我几乎已经到了,但您还需要添加IgnoreFirst和IgnoreLast属性。 Otherwise, the last two or three lines will cause an error to be thrown as they don't have enough columns for the layout. 否则,最后两三行将导致错误,因为它们的布局列不足。

I haven't used FileHelpers library. 我还没有使用FileHelpers库。 Never needed to. 没必要。 These manipulations are not difficult to do by myself. 这些操作我自己并不难。 What I would do is as simple as 1-2-3: 我要做的只是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(). 这个想法是使必填字段的添加成为Orders类的责任,而不是在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. Orders类需要具有仅从每一行具有的所有标记中读取所需标记的方法。 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. 每次您要阅读特定字段时, AddRequiredFields根据需要修改AddRequiredFields You would have already enumerated all the fields of csv file in the OrderFieldNumbers property. 您应该已经在OrderFieldNumbers属性中枚举了csv文件的所有字段。 Thus, you don't need to remember the positions of each field. 因此,您无需记住每个字段的位置。 You simply call the name as OrderFieldNumbers.myNeededColumnNumber and you get it. 您只需将名称命名为OrderFieldNumbers.myNeededColumnNumber获得。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM