gpt4 book ai didi

c# - 将单独的列从 .csv 拉入 c# 中的单独数组

转载 作者:行者123 更新时间:2023-11-30 15:32:15 28 4
gpt4 key购买 nike

这个项目的背景。一开始是一项简单的家庭作业,要求我存储 5 个邮政编码及其对应的城市。当用户在文本框中输入邮政编码时,会返回对应的城市,反之亦然。我编写了返回这些值的代码,但后来我决定将所有邮政编码及其对应的城市存储在外部 .csv 中,并将这些值存储在数组中并运行代码,因为如果它值得做,它的值(value)过头了!澄清一下,这不再是作业,只是为了学习更多关于在 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

等等大约 46,000 行。

如何将 zip 和 primary_city 拉入我的 for 循环可以运行的两个单独的数组(我猜测有一些“.Split”,“”行)?

此外,如果有更好的方法来解决这个问题,请告诉我(但请务必留下解释,因为我想了解您的出发点)。

最佳答案

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

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);
}

关于c# - 将单独的列从 .csv 拉入 c# 中的单独数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19408568/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com