[英]Trouble with getting data from a CSV file C#
我試圖從 CSV 文件中獲取有關客戶和他們想要的車輛的一些信息,然后將此信息放入我的程序中。 在 CSV 文件中有一個客戶名稱,然后在那里命名他們想要的車輛列表。
我已經嘗試過拆分線路,以便在我到達下一個客戶名稱后停止將車輛添加到我的列表中。
這是 CSV 文件中的數據,我想將客戶名稱存儲在我的客戶列表中,然后將名稱下的所有車輛存儲在客戶車輛列表中。
亞歷克斯
汽車,福特,GT40,1964,200000,987,紅色,A3,2,4,假車,福特,嘉年華,2001,2000,1015,藍色,fdssf1,4,1,真車,大眾,高爾夫,2007, 2000,1048,橙色,d3,5,1.8,真車,奧迪,A4,2015,20000,1870,黑色,23m,4,2,真卡車,豐田,苔原,2017,35000,2700,藍色,bbb123, 4000,4,2017 汽車,梅賽德斯,C220,2001,3000,1450,藍色,1klvr,5,2.2,True Plane,波音,707,1967,11000000,250000,藍色,r114,5,4000,1708噴射
傑克
Car,Koenigsegg,CCX,2008,1200000,1721,White,lkn,2,5,True Car,Pagani,Zonda F,2012,4200000,1520,White,5m,2,7.2,True Car,Ford,Cortina,1984 ,3700,1200,Grey,rrr5,4,1.6,False Car,Ford,Focus RS,2015,18000,1502,Black,erfwaew8,5,2,True Car,VolksWagon,Jetta,2000,5755,1321,Brown, ewr4,5,1.8,False Car,Audi,S8,2009,7500,3150,Green,fdasf7,4,4,True Plane,Supermarine,Spitfire,1942,510000,108000,Green,spft,False,40,20000, 2、道具
本
Car,Audi,A4,2004,4195,1850,Green,fd11,5,2.8,True Car,BMW,M4,2018,62000,2005,Matt Black,aa34,5,4,True Car,Mercedes,C220,2015 ,24000,1440,White,asp98,5,2.2,True Plane,Schempp-Hirth,Janus C,2000,45000,750,White,dsfsd22,True,1200,10000,2,None
伊娃
直升機,Robinson,R22,1995,120000,1500,Black,22222f,False,150,15000 飛機,Supermarine,Spitfire,1939,8000000,14000,Green,ffff3,False,8000,4,Propa Car RS3,2018,54000,1995,Coral,dsf23,5,4.2,True Car,BMW,M4,2017,48000,2018,Pink,fdsgd1,5,4,True Truck,Ford,F150,2016,18000,190灰色,f15044t,4000,4,2017
List<string> listofCustomers = new List<string>();
List<List<baseVehicle>> customerVehicles = new List<List<baseVehicle>>();
string[] lines = File.ReadAllLines("Customer.CSV");
string customer = "";
customer = lines[0];
listofCustomers.Add(customer);
customerVehicles.Add(new List<baseVehicle>());
for (int i = 1; i < lines.Length; i++)
{
string[] bits = lines[i].Split(',');
if (bits[0].ToUpper() == "Car".ToUpper())
{
Car Car = new Car(bits[0], bits[1], bits[2], int.Parse(bits[3]), int.Parse(bits[4]), int.Parse(bits[5]), bits[6], bits[7], int.Parse(bits[8]), double.Parse(bits[9]), bool.Parse(bits[10]));
customerVehicles.Last().Add(Car);
}
if (bits[0].ToUpper() == "Truck".ToUpper())
{
Truck Truck = new Truck(bits[0], bits[1], bits[2], int.Parse(bits[3]), int.Parse(bits[4]), int.Parse(bits[5]), bits[6], bits[7], int.Parse(bits[8]), int.Parse(bits[9]), int.Parse(bits[10]));
customerVehicles.Last().Add(Truck);
}
if (bits[0].ToUpper() == "Helicopter".ToUpper())
{
Helicopter Helicopter = new Helicopter(bits[0], bits[1], bits[2], int.Parse(bits[3]), int.Parse(bits[4]), int.Parse(bits[5]), bits[6], bits[7], bool.Parse(bits[8]), int.Parse(bits[9]), int.Parse(bits[10]));
customerVehicles.Last().Add(Helicopter);
}
if (bits[0].ToUpper() == "Plane".ToUpper())
{
Plane Plane = new Plane(bits[0], bits[1], bits[2], int.Parse(bits[3]), int.Parse(bits[4]), int.Parse(bits[5]), bits[6], bits[7], bool.Parse(bits[8]), int.Parse(bits[9]), int.Parse(bits[10]), int.Parse(bits[11]), bits[12]);
customerVehicles.Last().Add(Plane);
}
else if(bits[0] == "" )
{
return;
}
}
我同意你應該拉一個圖書館來為你做這項工作。
如果由於某種原因這不是一個選項,您應該首先尋找您的客戶,然后在單獨的功能中處理車輛。 當您嘗試一次性完成所有工作時,您的心理開銷會很高。 只是將客戶拆分出來可能看起來像這樣:
using Data;
using System;
using System.Collections.Generic;
namespace TestPaths
{
public class Customer
{
public string Name { get; set; }
public List<string> Vehicles { get; set; }
}
class Program
{
static void Main(string[] args)
{
var customers = new List<Customer>();
Console.WriteLine("Hello World!");
var inputFile = File.ReadAllLines("Customer.CSV");
var customer = new Customer
{
Name = inputFile[0], //the first line is the first customer
Vehicles = new List<string>()
};
for (int i = 2; i < inputFile.Length; i++)
{
if (!string.IsNullOrWhiteSpace(inputFile[i]))
{
customer.Vehicles.Add(inputFile[i]);
} else
{
customers.Add(customer);
if(i++<inputFile.Length)
customer = new Customer
{
Name = inputFile[i], //the name is one past the blank
Vehicles = new List<string>()
};
i++; //skip the next blank
}
}
customers.Add(customer); //add in the last customer
foreach (var cust in customers)
{
Console.WriteLine(cust.Name);
}
Console.ReadLine();
}
}
}
由於這不是標准的 CSV 文件格式,因此您不能使用 CSV 庫。
我會通過測試一行上是否正好有一位來檢測客戶,然后將新的客戶和車輛列表添加到列表中。 此外,我們必須跳過空行。 switch 語句比許多 if 語句更具可讀性。
List<string> listofCustomers = new List<string>();
List<List<VehicleBase>> customerVehicles = new List<List<VehicleBase>>();
List<VehicleBase> vehicles = null;
foreach (string line in File.ReadLines(csvPath)) {
string[] bits = line.Split(',');
if (bits.Length == 1 && bits[0] != "") { // We have a new customer
listofCustomers.Add(bits[0]);
vehicles = new List<VehicleBase>();
customerVehicles.Add(vehicles);
} else if (bits.Length > 8) { // Not an empty line
switch (bits[0].ToUpper()) {
case "CAR":
var car = new Car(bits[0], bits[1], bits[2], int.Parse(bits[3]), int.Parse(bits[4]), int.Parse(bits[5]), bits[6], bits[7], int.Parse(bits[8]), double.Parse(bits[9]), bool.Parse(bits[10]));
vehicles.Add(car);
break;
case "TRUCK":
var truck = new Truck(bits[0], bits[1], bits[2], int.Parse(bits[3]), int.Parse(bits[4]), int.Parse(bits[5]), bits[6], bits[7], int.Parse(bits[8]), int.Parse(bits[9]), int.Parse(bits[10]));
vehicles.Add(truck);
break;
case "HELICOPTER":
var helicopter = new Helicopter(bits[0], bits[1], bits[2], int.Parse(bits[3]), int.Parse(bits[4]), int.Parse(bits[5]), bits[6], bits[7], bool.Parse(bits[8]), int.Parse(bits[9]), int.Parse(bits[10]));
vehicles.Add(helicopter);
break;
case "PLANE":
var plane = new Plane(bits[0], bits[1], bits[2], int.Parse(bits[3]), int.Parse(bits[4]), int.Parse(bits[5]), bits[6], bits[7], bool.Parse(bits[8]), int.Parse(bits[9]), int.Parse(bits[10]), int.Parse(bits[11]), bits[12]);
vehicles.Add(plane);
break;
}
}
}
我也改變了命名。 在 C# 中,類名有 PascalCase 和局部變量 camelCase。
File.ReadLines
返回一個IEnumerable<string>
(而不是像File.ReadAllLines
那樣的字符串數組)並在 foreach 循環進行時讀取文件,而不是將所有行存儲到數組中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.