c# - 从 CSV 文件 C# 获取数据的问题
问题描述
我正在尝试从 CSV 中获取有关客户和他们想要的车辆的一些信息,然后将这些信息放入我的程序中。在 CSV 文件中有一个客户名称,然后在下面列出他们想要的车辆列表。
我已经尝试过拆分线路,以便在我到达下一个客户姓名后停止将车辆添加到我的列表中。
这是 CSV 文件中的数据,我想将客户名称存储在我的客户列表中,然后将所有车辆存储在客户车辆列表中。
亚历克斯
汽车,福特,GT40,1964,200000,987,红色,A3,2,4,假车,福特,嘉年华,2001,2000,1015,蓝色,fdssf1,4,1,真车,大众旅行车,高尔夫,2007, 2000,1048,Orange,d3,5,1.8,True Car,Audi,A4,2015,20000,1870,Black,23m,4,2,True Truck,Toyota,Tundra,2017,35000,2700,Blue,bbb123, 4000,4,2017 Car,Mercedes,C220,2001,3000,1450,Blue,1klvr,5,2.2,True Plane,Boeing,707,1967,11000000,250000,Blue,r11ner,True,17845,41000,200,喷射
杰克
Car,Koenigsegg,CCX,2008,1200000,1721,白色,lkn,2,5,True Car,Pagani,Zonda F,2012,4200000,1520,White,5m,2,7.2,True Car,Ford,Cortina,1984 ,3700,1200,灰色,rrr5,4,1.6,假车,福特,福克斯RS,2015,18000,1502,黑色,erfwaew8,5,2,真车,大众,捷达,2000,5755,1321,棕色, 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、道具
本
轿车,奥迪,A4,2004,4195,1850,绿色,fd11,5,2.8,真车,宝马,M4,2018,62000,2005,马特黑,aa34,5,4,真车,奔驰,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
伊娃
直升机,罗宾逊,R22,1995,120000,1500,黑色,22222f,False,150,15000 飞机,Supermarine,喷火,1939,8000000,14000,绿色,ffff3,False,8000,20000,4,道具车,奥迪, 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,1900,灰色,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();
}
}
}
推荐阅读
- javascript - 有没有办法将脚本 php 传递给 jquery?
- python - 不确定如何在单元测试中发布表单数据
- reactjs - 在 React.js 中删除项目时的意外行为
- postgresql - 获取 JSONB 文档的一部分,同时保持结构完整
- php - 在实时搜索中传递所选产品的价值
- python - 使用一系列数字打印函数的结果
- python - 如何在张量流中获得特定值和形状的张量?
- java - Java FX TranslateTransition 持续时间更改
- android - 如何检测android上的屏幕亮度范围?
- javascript - node.js:如何将多个变量传递到视图中?