【问题标题】:How to remove the header and entire column from CSV file using C#?如何使用 C# 从 CSV 文件中删除标题和整列?
【发布时间】:2020-06-08 12:54:11
【问题描述】:
我正在将 JSON 文件转换为 CSV。转换完成后,我需要检查没有任何行的值的列。如果是这种情况,则应将整个列与标题一起删除。
在上面的示例中,“Retiring Period”列在其任何行中都没有任何值。因此,更新后的 CSV 应如下所示。
这是使用 C# 的预期和需要完成的。对此的任何帮助都将非常重要。
【问题讨论】:
标签:
c#
.net
csv
.net-core
【解决方案1】:
通用解决方案可能类似于
class Program {
static void Main (string[] args) {
GenerateNonEmptyCSV ("data.json", "data", "output.csv");
}
public static void GenerateNonEmptyCSV (string inputJsonFilePath_, string arrayName_, string outputFilePath_) {
//Read data from json file
DataSet dataSet;
using (TextReader tr = new StreamReader (inputJsonFilePath_)) {
dataSet = JsonConvert.DeserializeObject<DataSet> (tr.ReadToEnd ());
}
DataTable dataTable = dataSet.Tables[arrayName_];
//Get Valid column index into a hashset
var validColumns = new HashSet<int> ();
foreach (DataRow row in dataTable.Rows) {
if (validColumns.Count == dataTable.Columns.Count) { break; } //All columns are valid, no need to loop through rows anymore
for (int columnIndex = 0; columnIndex < dataTable.Columns.Count; columnIndex++) {
if (validColumns.Contains (columnIndex)) { continue; }
if (!string.IsNullOrWhiteSpace (row?.ItemArray[columnIndex]?.ToString ())) { validColumns.Add (columnIndex); }
}
}
//output valid columns into csv file
using (TextWriter tw = new StreamWriter (outputFilePath_)) {
string[] columnData = new string[validColumns.Count];
int index = 0;
foreach (int columnIndex in validColumns) {
columnData[index++] = dataTable.Columns[columnIndex].ColumnName;
}
tw.WriteLine (string.Join (",", columnData)); //Write column header
foreach (DataRow row in dataTable.Rows) {
string[] rowData = new string[validColumns.Count];
index = 0;
foreach (int columnIndex in validColumns) {
rowData[index++] = row?.ItemArray[columnIndex]?.ToString ();
}
tw.WriteLine (string.Join (",", rowData));
}
}
}
}
使用的样本数据
{
"data": [
{
"EmployeeId": "1",
"EmployeeName": "Name1",
"RetiringPeriod": "",
"Salary":"80k"
},
{
"EmployeeId": "2",
"EmployeeName": "Name2",
"RetiringPeriod": "",
"Salary":"60k"
}
]
}
【解决方案2】:
假设您知道如何从集合移到 .CSV 文件并返回,并且您有一个包含您的数据(包括空的 Retirement)的员工集合,您可以生成一个EmployeesWithoutRetirements 集合并将其保存为 .CSV。
void Main()
{
var employeeWithoutRetirements = (List<EmployeeWithoutRetirement>) Employees
.Select(x => new EmployeeWithoutRetirement {
EmployeeID = x.EmployeeID,
EmployeeName = x.EmployeeName,
Salary = x.Salary });
}
class EmployeeWithoutRetirement
{
public int EmployeeID {get;set;}
public string EmployeeName {get;set;}
public decimal Salary { get; set; }
}