需要帮助编辑 CSV 文件中的字段?
Need help editing fields inside of a CSV file?
我可以编辑每个字段,但这只适用于 CSV 文件的第一行。如果我尝试输入第二行或第三行等的患者 ID,它不会编辑任何数据。有人知道这是为什么吗?
我将这些行存储在一个字符串中,然后将它们分成另一个字符串中的不同字段,使用了一个 for 循环,我认为它会遍历每一行直到找到 ID。
{
string[] fields = lines[i].Split(','); // this here is splitting up each field, so ID,Name, Age will have their own index value
if (fields[i] == ID) // this is where you will make the change, we need to determine the field here
{
for (int k = 0; k <= fields.Length; k++)
{
if(k == choice)
{
fields[k-1] = change;
Write.WriteLine(fields[0] + "," + fields[1] + "," + fields[2] + "," + fields[3] + "," + fields[4] + "," + fields[5]); // change is made
}
}
}
else
{
if (!edited)
{
Write.WriteLine(fields[0] + "," + fields[1] + "," + fields[2]+ "," + fields[3] + "," + fields[4] + "," + fields[5]); // if not edited then it will print the line to the temp file
}
}
}
Write.Close();
File.Delete("patinfo.csv");
System.IO.File.Move("temp.csv", "patinfo.csv");
Console.ReadLine();
if (fields[0] == ID) // replaced i with 0
{
for (int k = 1; k <= fields.Length; k++) // replaced 0 with 1
提示:检查'change',如果不在[1,6]中,则该行未写入
我可以编辑每个字段,但这只适用于 CSV 文件的第一行。如果我尝试输入第二行或第三行等的患者 ID,它不会编辑任何数据。有人知道这是为什么吗?
我将这些行存储在一个字符串中,然后将它们分成另一个字符串中的不同字段,使用了一个 for 循环,我认为它会遍历每一行直到找到 ID。
{
string[] fields = lines[i].Split(','); // this here is splitting up each field, so ID,Name, Age will have their own index value
if (fields[i] == ID) // this is where you will make the change, we need to determine the field here
{
for (int k = 0; k <= fields.Length; k++)
{
if(k == choice)
{
fields[k-1] = change;
Write.WriteLine(fields[0] + "," + fields[1] + "," + fields[2] + "," + fields[3] + "," + fields[4] + "," + fields[5]); // change is made
}
}
}
else
{
if (!edited)
{
Write.WriteLine(fields[0] + "," + fields[1] + "," + fields[2]+ "," + fields[3] + "," + fields[4] + "," + fields[5]); // if not edited then it will print the line to the temp file
}
}
}
Write.Close();
File.Delete("patinfo.csv");
System.IO.File.Move("temp.csv", "patinfo.csv");
Console.ReadLine();
if (fields[0] == ID) // replaced i with 0
{
for (int k = 1; k <= fields.Length; k++) // replaced 0 with 1
提示:检查'change',如果不在[1,6]中,则该行未写入