使用 filehelpers 只读取 csv 文件的某些列
Reading only certain columns of a csv file with filehelpers
试图只读取我的 csv
文件的这些列: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 文件:
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",".00","[=10=].00","[=10=].00","[=10=].00","","0.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",".00",".00","[=10=].00","[=10=].00","","3.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",".00",".00","[=10=].00","[=10=].00","",".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
不确定如何跳过我不想要的字段并仅添加我想要的字段。我想我可以创建虚拟字段来读取 csv 文件,然后在之后对这些项目执行删除,但是有没有办法从一开始就不包含它们?我认为最后两行也会产生错误,我该如何处理它们?这只是我的一小部分代码:
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; }
}
}
仍然无法读取文件这里是我更改代码的方式:
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; }
}
您快完成了,但我认为您还需要添加 IgnoreFirst 和 IgnoreLast 属性。否则,最后两三行将导致抛出错误,因为它们没有足够的列用于布局。
我还没有使用过 FileHelpers
库。从来不需要。这些操作我自己做起来并不难。我要做的就像 1-2-3 一样简单:
- 一次读一行;
- 拆分线并获得令牌;
- 只取必填字段数组中提到的标记。
想法是 添加 必需的 字段 是订单 class 的责任,而不是为其编写逻辑在 Main().
在代码-伪代码组合中,它看起来像下面这样:
在 Main 方法中
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);
}
}
}
在订单中class
class Orders
需要有一种方法,该方法只会从每行具有的所有标记中读取所需的标记。这将是:
//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];
.
.
.
}
每次要读取特定字段时,根据需要修改AddRequiredFields
。您应该已经在 OrderFieldNumbers
属性 中枚举了 csv 文件的所有字段。因此,您不需要记住每个字段的位置。您只需将名称命名为 OrderFieldNumbers.myNeededColumnNumber
即可。
试图只读取我的 csv
文件的这些列: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 文件:
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",".00","[=10=].00","[=10=].00","[=10=].00","","0.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",".00",".00","[=10=].00","[=10=].00","","3.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",".00",".00","[=10=].00","[=10=].00","",".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
不确定如何跳过我不想要的字段并仅添加我想要的字段。我想我可以创建虚拟字段来读取 csv 文件,然后在之后对这些项目执行删除,但是有没有办法从一开始就不包含它们?我认为最后两行也会产生错误,我该如何处理它们?这只是我的一小部分代码:
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; }
}
}
仍然无法读取文件这里是我更改代码的方式:
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; }
}
您快完成了,但我认为您还需要添加 IgnoreFirst 和 IgnoreLast 属性。否则,最后两三行将导致抛出错误,因为它们没有足够的列用于布局。
我还没有使用过 FileHelpers
库。从来不需要。这些操作我自己做起来并不难。我要做的就像 1-2-3 一样简单:
- 一次读一行;
- 拆分线并获得令牌;
- 只取必填字段数组中提到的标记。
想法是 添加 必需的 字段 是订单 class 的责任,而不是为其编写逻辑在 Main().
在代码-伪代码组合中,它看起来像下面这样:
在 Main 方法中
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);
}
}
}
在订单中class
class Orders
需要有一种方法,该方法只会从每行具有的所有标记中读取所需的标记。这将是:
//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];
.
.
.
}
每次要读取特定字段时,根据需要修改AddRequiredFields
。您应该已经在 OrderFieldNumbers
属性 中枚举了 csv 文件的所有字段。因此,您不需要记住每个字段的位置。您只需将名称命名为 OrderFieldNumbers.myNeededColumnNumber
即可。