【发布时间】:2020-08-20 22:16:11
【问题描述】:
我有一个如下所示的 CSV 文件:
,Location_Code,Location_Desc,Type_Code,Fault_type,Prod_Number,Model,Causer,Auditor,Prio,Capture_Date,Steer,Engine,Country,Current shift number,VIN,Comment,Shift,Year,Fault location C_Code,Fault location C_Desc,Fault type C_Code,Fault type C_Desc,Comment R,Baumuster Sales desc.,Baumuster Technical desc.,T24
0,09122,Engine,42,Poor fit,7117215,W205,Final 3,"Plant 1, WSA",0,2019-04-05,1,83,705,T1220190404T0092,55SWF8DB7KU316971,,A,2019,,,,,,C 300,205 E20 G,
1,09122,Engine,42,Poor fit,7117235,W205,Final 3,"Plant 1, WSA",0,2019-04-05,1,83,705,T1220190404T0122,55SWF8DB2KU316991,,A,2019,,,,,,C 300,205 E20 G,
2,09122,Transmission,42,Poor fit,7117237,W205,Final 3,"Plant 1, WSA",0,2019-04-05,1,83,705,T1220190404T0126,55SWF8DB6KU316993,,A,2019,,,,,,C 300,205 E20 G,
我想编写代码,在对选定列的单词进行标记后获取选定列标题的单词计数(在字典样式的键值对中)。 我还想保持字数按值降序排序。 例如。
Location_Desc
Engine: 2
Transmission: 1
这是我目前的代码:
int colNumber;
for(colNumber=0; colNumber<columns.Length; colNumber++)
{
if ( columns[colNumber].Equals(columnHeader))
{
break;
}
}
Debug.WriteLine("Column Number: " + colNumber);
for(int i=0; i<inputCsv.Length; i++)
{
string[] row = inputCsv[i].Split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
string column = row[colNumber];
Debug.WriteLine(row.ToString());
}
我能够通过 for 循环获取列标题名称,但我不仅无法忽略引号内的逗号,而且无法从列标题中获取值(在 Python 的 Pandas 中也称为 Series )。
非常感谢您的帮助!
【问题讨论】:
-
有很多库可以读取 CSV 文件。例如,CsvHelper。考虑使用它来读取 CSV 文件。这将为您节省大量时间。
-
I want to write code that gets the word count of words of a selected column header after tokenizing the words of the selected column (in dictionary style key-value pairs).我有一个关于你这部分问题的问题。您的样本中的列Fault_type的预期结果是什么:1. 一对("Poor fit": 3)或2. 两对("Poor": 3)和("fit": 3)? -
@IliarTurdushev 是一对
标签: c# string multidimensional-array tokenize word-count