尝试仅读取csv
文件的这些列:Buyer Fullname
,Ship to Address1
,Ship to Address2
,Ship to City
,Ship to State
,{{ 1}},Ship to Zip
,Ship to Country
,Item Title
,Quantity
,Sale Price
。
这是我的.CSV文件:
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
仍然无法在此处阅读文件是我更改代码的方式:
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; }
}
}
答案 0 :(得分:1)
你差不多了,但我还需要添加IgnoreFirst和IgnoreLast属性。否则,最后两行或三行将导致错误,因为它们没有足够的列用于布局。
答案 1 :(得分:1)
我没有使用FileHelpers
库。永远不需要。这些操作本身并不难。我会做的就像1-2-3一样简单:
这个想法是让添加必需的字段成为Orders类的责任,而不是在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
即可获得。