使用filehelper仅读取csv文件的某些列
本文关键字:文件 csv filehelper 读取 使用 | 更新日期: 2023-09-27 18:11:40
尝试仅读取我的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","$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
不知道如何跳过我不想要的字段,只添加我想要的字段。我想我可以创建虚拟字段在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:
- 每次读取一行;
- 分行获取token;
- 只取必需字段数组中提到的令牌。
这个想法是使添加必需的字段成为Orders类的职责,而不是在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);
}
}
}
类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
,就可以获得它。