将单独的列从.csv拉到单独的数组在c#

这个项目的背景。 它开始作为一个简单的家庭作业,要求我存储5个邮政编码及其相应的城市。 当用户在一个文本框中input一个邮政编码时,就会返回一个相应的城市,同样的情况也可以完成。 我编写了返回这些值的代码,但是后来我决定将所有的邮政编码及其相应的城市存储在一个外部的.csv文件中,并将这些值存储在数组中,然后运行代码,因为如果值得这样做的话,矫枉过正! 为了澄清,这不再是功课,只是为了了解更多关于在C#中使用外部文件。

在下面的代码中,我调用了成功打开文件,现在我只需要帮助弄清楚如何提取存储在两个单独列(一个用于城市,一个用于邮政编码)中的数据,并将它们存储在两个数组中由for循环来执行。 这是我现在的代码。 你可以看到我以前如何在数组中存储其他值并将其提取出来:

public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnConvert2City_Click(object sender, EventArgs e) { try { string dir = System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().Location); string path = dir + @"\zip_code_database_edited.csv"; var open = new StreamReader(File.OpenRead(path)); int EnteredZipcode = Convert.ToInt32(txtZipcode.Text.Trim()); string result = "No Cities Found"; string[] Cities = new String[5] { "FLINTSTONE", "JAMAICA", "SCHENECTADY", "COTTONDALE", "CINCINNATI" }; int[] Zipcode = new int[5] { 30725, 11432, 12345, 35453, 45263 }; for (int i = 0; i <= Zipcode.Length - 1; i++) { if (Zipcode[i] == EnteredZipcode) { result = Cities[i]; break; } } string DisplayState = result; txtCity.Text = DisplayState; } catch (FormatException) { MessageBox.Show("Input must be numeric value."); } catch (OverflowException) { MessageBox.Show("Zipcode to long. Please Re-enter"); } } private void btnConvert2Zipcode_Click(object sender, EventArgs e) { string dir = System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().Location); string path = dir + @"\zip_code_database_edited.csv"; var open = new StreamReader(File.OpenRead(path)); String EnteredCity = txtCity.Text.ToUpper(); string result = "No Zipcode Found"; string[] Cities = new String[5] { "FLINTSTONE", "JAMAICA", "SCHENECTADY", "COTTONDALE", "CINCINNATI" }; int[] Zipcode = new int[5] { 30725, 11432, 12345, 35453, 45263 }; for (int i = 0; i <= Cities.Length - 1; i++) { if (Cities[i] == EnteredCity) { result = Convert.ToString(Zipcode[i]); break; } } string DisplayZip = result; txtZipcode.Text = DisplayZip; } } 

以下数据是我的excel .csv中的数据的片段:

 zip,primary_city 44273,Seville 44274,Sharon Center 44275,Spencer 44276,Sterling 44278,Tallmadge 44280,Valley City 44281,Wadsworth 44282,Wadsworth 44285,Wayland 

等约46000行。

我怎样才能拉zip和primary_city到两个单独的数组(我猜一些“分裂”,“行),我的for循环可以操作?

此外,如果有更好的方法去做这件事,请让我知道(但请务必留下一个解释,因为我想知道你来自哪里)。

不要创build两个单独的数组。为城市创build一个单独的类

 class City { public string Name{get;set;} public int ZipCode{get;set;} } 

现在从该CSV文件读取数据

 List<City> cities=File.ReadAllLines(path) .Select(x=>new City { ZipCode=int.Parse(x.Split(',')[0]), Name=x.Split(',')[1] }).ToList<City>(); 

或者你可以做到这一点

  List<City> cities=new List<City>(); foreach(String s in File.ReadAllLines(path)) { City temp=new City(); temp.ZipCode=int.Parse(s.Split(',')[0]); temp.Name=s.Split(',')[1]; cities.Add(temp); } 

你可以试试这个:

  string dir = System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().Location); string path = dir + @"\zip_code_database_edited.csv"; var open = new StreamReader(File.OpenRead(path)); var cities = new HashList<string>(); var zipCodes = new HashList<int>(); var zipAndCity = new string[2]; string line = string.Empty; using (open) { while ((line = reader.ReadLine()) != null) { zipAndCity = line.Split(","); zipCodes.Add(int.Parse(zipAndCity[0])); cities.Add(zipAndCity[1]); } } 

我张贴这个答案了解更多关于C#因为我发布了这个问题。 读取CSV时,有比String.Split()更好的选项。

.NET Framework已经有一个名为TextFieldParser的内置专用CSVparsing器。

它位于Microsoft.VisualBasic.FileIO名称空间中。

不仅有很多边缘情况, String.Split()没有适当的configuration来处理,但它也是使用StreamReader更慢。