c# - C# CSV 解析和隐藏具有真值或假值的列或行
问题描述
如何使控制台应用程序读取具有 IsHidden 行的 csv 文件(isHidden = false 以显示)
关键是我已经启动并运行了所有内容,但无法想到将 true(hidden) 和 false(true) 行读入控制台应用程序并向其展示应该显示的人的逻辑 :D - 对不起我的英语不好:)
我正在使用的代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PreInterviewTask
{
class Program
{
static void Main(string[] args)
{
// Get the data from path.
string sampleCSV = @"C:\Users\Tomas\source\repos\PreInterviewTask\PreInterviewTask\HistoricalData\HistoricalData.csv";
string[,] values = LoadCSV(sampleCSV);
int num_rows = values.GetUpperBound(0) + 1;
int num_cols = values.GetUpperBound(1) + 1;
// Display the data to show we have it.
for (int c = 0; c < num_cols; c++)
Console.Write(values[0, c] + "\t");
//Read the data.
for (int r = 1; r < num_rows; r++)
{
// dgvValues.Rows.Add();
Console.WriteLine();
for (int c = 0; c < num_cols; c++)
{
Console.Write(values[r, c] + "\t");
}
}
Console.ReadLine();
}
private static string[,] LoadCSV(string filename)
{
// Get the file's text.
string whole_file = System.IO.File.ReadAllText(filename);
// Split into lines.
whole_file = whole_file.Replace('\n', '\r');
string[] lines = whole_file.Split(new char[] { '\r' },
StringSplitOptions.RemoveEmptyEntries);
// See how many rows and columns there are.
int num_rows = lines.Length;
int num_cols = lines[0].Split(',').Length;
// Allocate the data array.
string[,] values = new string[num_rows, num_cols];
// Load the array.
for (int r = 0; r < num_rows; r++)
{
string[] line_r = lines[r].Split(',');
for (int c = 0; c < num_cols; c++)
{
values[r, c] = line_r[c];
}
}
// Return the values.
return values;
}
}
}
我得到的输出:
ID;MenuName;ParentID;isHidden;LinkURL
1;Company;NULL;False;/company
2;About Us;1;False;/company/aboutus
3;Mission;1;False;/company/mission
4;Team;2;False;/company/aboutus/team
5;Client 2;10;False;/references/client2
6;Client 1;10;False;/references/client1
7;Client 4;10;True;/references/client4
8;Client 5;10;True;/references/client5
10;References;NULL;False;/references
应该是什么样子:示例输出
. Company
.... About Us
....... Team
.... Mission
. References
.... Client 1
.... Client 2
解决方案
看看以下是否有帮助。我使用您的输出作为输入,因为我没有实际输入。:
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication176
{
class Program
{
const string FILENAME = @"c:\temp\test.csv";
static void Main(string[] args)
{
Menu menu = new Menu(FILENAME);
List<Menu> sortedRows = Menu.items.OrderBy(x => x).ToList();
menu.Print(sortedRows);
Console.ReadLine();
}
}
public class Menu : IComparable
{
public static List<Menu> items { get; set; }
public int ID { get; set; }
public string name { get; set; }
public int? parent { get; set; }
public Boolean hidden { get; set; }
public string[] linkUrl { get; set; }
public Menu() { }
public Menu(string filename)
{
StreamReader reader = new StreamReader(filename);
string line = "";
int rowCount = 0;
while ((line = reader.ReadLine()) != null)
{
line = line.Trim();
if (line.Length > 0)
{
if (++rowCount == 1)
{
items = new List<Menu>();
}
else
{
Menu newMenu = new Menu();
items.Add(newMenu);
string[] splitArray = line.Split(new char[] { ';' }).ToArray();
newMenu.ID = int.Parse(splitArray[0]);
newMenu.name = splitArray[1];
newMenu.parent = (splitArray[2] == "NULL")? null : (int?)int.Parse(splitArray[2]);
newMenu.hidden = Boolean.Parse(splitArray[3]);
newMenu.linkUrl = splitArray[4].Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
}
}
}
}
public int CompareTo(object obj)
{
Menu other = (Menu)obj;
int min = Math.Min(this.linkUrl.Length, other.linkUrl.Length);
for (int i = 0; i < min; i++)
{
int compare = this.linkUrl[i].CompareTo(other.linkUrl[i]);
if (compare != 0) return compare;
}
return this.linkUrl.Length.CompareTo(other.linkUrl.Length);
}
public void Print(List<Menu> rows)
{
foreach (Menu menu in rows)
{
if (!menu.hidden)
{
int length = menu.linkUrl.Length - 1;
Console.WriteLine(".{0} {1}", new string('.', 3 * length), menu.name);
}
}
}
}
}
推荐阅读
- java - 对警告“无法解析变量”感到困惑
- ruby-on-rails - 克隆、捆绑、db:migrated 时在 GitHub 中工作的 rails 应用程序,然后在不同的机器上失败。为什么”
- ruby - 为什么继承单例方法(AKA:类方法)?
- c - pthread_rwlock 的优先级?
- sparql - SPARQL 查询未产生预期结果
- jquery - jQuery 在收音机选择和/或页面加载时隐藏/显示 div
- javascript - 如何使用 React Hooks 仅在用户访问的任何第一页设置状态?
- regex - 用于匹配一年前的字符串的正则表达式
- c# - 处理 HTTPClient 在火中执行多个请求并忘记
- python - Python包:内容相互导入