简体   繁体   English

从C#中的特定列中提取文本?

[英]Extract text from specific columns in c#?

I have been working on extracting text from a csv file and store the data in a string. 我一直在努力从csv文件中提取文本并将数据存储在字符串中。 But now, I would like to extract text from some of the specific columns and store the data in a string.I would like the wordDocContents variable to contain the specific columns and the data in those specific columns which is bank_account , bank_name , customer_name . 但是现在,我想从某些特定列中提取文本并将数据存储在wordDocContents我希望wordDocContents变量包含特定列以及这些特定列中的数据,例如bank_accountbank_namecustomer_name Currently, my wordDocContents has the entire data from my csv file. 目前,我的wordDocContents具有来自csv文件的全部数据。 Is there a way to filter out the specific columns and the data in those columns and store it in the variable wordDocContents . 有没有一种方法可以过滤出特定的列和这些列中的数据,并将其存储在变量wordDocContents Thanks 谢谢

Here is what I tried so far - 这是我到目前为止尝试过的-

public void button1Clicked(object sender, EventArgs args)
{
    button1.Text = "You clicked me";

    var textExtractor = new TextExtractor();

    var wordDocContents = textExtractor.Extract("t.csv");
    Console.WriteLine(wordDocContents);
    Console.ReadLine();
}

The contents of wordDocContents:- wordDocContents的内容:

ACCOUNT_NUMBER,CUSTOMER_NAMES,VALUE_DATE,BOOKING_DATE,TRANSACTION,ACCOUNT_TYPE,BALANCE_TYPE,REFERENCE,MONEY.OUT,MONEY.IN,RUNNING.BALANCE,BRANCH,EMAIL,ACTUAL.BALANCE,AVAILABLE.BALANCE
1000000001,TEST,,2847899,KES,Account,,,10/10/2016,9/11/2016,15181800,UPPER HILL BRANCH,another@yahoo.com,5403.75,5403.75,
1000000001,,9/11/2016,9/11/2016,Opening Balance,,,,,,4643.22,,,,,
1000000001,,12/10/2016,12/10/2016,Mobile Mpesa Transfer,,,,1533,,3110.22,,,,,
1000000001,,17-10-2016,17-10-2016,ATM Withdrawal,,,6.29006E+11,1000,,2110.22,,,,,
1000000001,,17-10-2016,17-10-2016,ATM Withdrawal,,,6.29118E+11,2000,,110.22,,,,,
1000000001,,17-10-2016,17-10-2016,Mobile Mpesa Transfer,,,,2083,,-1972.78,,,,,
1000000001,,17-10-2016,17-10-2016,Transfer from Mpesa,,,,0,4000,2027.22,,,,,
1000000001,,18-10-2016,18-10-2016,Mobile Mpesa Transfer,,,,333,,1694.22,,,,,

From my knowledge on how csv files are constructed. 根据我对csv文件构造方式的了解。 (Maybe post the first 2 lines of your output?) (也许发布您输出的前两行?)

string[] lines = wordDocContents.Split("\n");
string[] columns = lines[0].Split(",");
string[][] data = new string[lines.Length][columns.Length];

Now let's say customer_name is under columns[2], you can try to: 现在,假设customer_name在columns [2]下,您可以尝试:

List<string> customerNames = new List<string>();
for (int i = 1; i < lines.Length; i++) {
customerNames.Add(data[i][2]);
}

Edit just saw the output, this code might need some adjusting for your particular case. 编辑只是看到输出,此代码可能需要针对您的特定情况进行一些调整。 I am not 100% sure if string.Split(",") works for multiple commas in a row, but it's worth a shot. 我不确定100%是否将string.Split(“,”)连续用于多个逗号,但是值得一试。 Just change the [2] to whichever column you need. 只需将[2]更改为所需的任何列即可。

It should be going from [0],[1],[2] etc. 它应该从[0],[1],[2]等处开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM