开发者

Extracting specific data from a file

I have a particular csv file with the following data:

uid Business Area   employeeNumber  First   Name    Last Name   JobcodeID

EXBK145 Home Loans  9134805 Mason   Kapari  50635804
ABES623 Absa Consultants & Actuaries    9136459 Elroy   Shilling    50644965
EXTK194 Forex Operations    9140600 Tania   Kanni开发者_如何学Goappen  60390922
ABNM539 Absa  Business Bank 9025659 Zodwa   Mgaga   60225348
ABAJ253 Absa Ins&Fin Advisers(Pty)Ltd   9040745 André   Joubert 50055313
ABSP391 Channel Sales & Service 9044366 Steph   Potgieter   4863
ABDL205 Absa Card   9083713 Lesego  Lekgoro 50050702
EXNM450 Channel Sales & Service 9121175 Thando  Shangase    60470761
EXSK201 Allpay  9130647 Sibusiso    Kambule 60615651
ABEK171 AIFA-PB Advisers    9011833 Esmé    Pretorius   60660026

I need to have the following output:

Action  uid       EmploymentStatus
modify  EXBK145   0
modify  ABES623   0
modify  EXTK194   0
modify  ABNM539   0
modify  ABAJ253   0
modify  ABSP391   0
modify  ABDL205   0
modify  EXNM450   0
modify  EXSK201   0
modify  ABEK171   0

from the input file all i need is the UID. Ive tried the "fast csv reader" but i doesn't work properly on my system........... I'm using visual C# 2010. I have also tries regular expressions but that didn't work as expected.

Any help will be appreciated and any examples so i can learn from this.

Thanks in advance Kurt


What you have is a tab-delimited file. There are many free and open source libraries which can read them (for example, FileHelpers).

On the other hand, if you only need the first column, you can read the file line by line and parse it yourself using a StreamReader:

 using (StreamReader sr = new StreamReader("TabDelimited.txt")) 
 {
      string line;
      // Read and display lines from the file until the end of 
      // the file is reached.
      while ((line = sr.ReadLine()) != null) 
      {
          int idx = line.IndexOf(' ');
          if (idx < 0)
             continue;

          // get the uid
          string uid = line.Substring(0, idx - 1);
      }
 }


You can read the whole file using File.ReadAllLines(path) in a string array and then parse each line using the String.split(' ').

string [] strArray = File.ReadAllLines(path);
string [] line1 =strArray[0].Split(' ');

and line1 will have all the data of the first row.

Thanks Saurabh


There is a good utility called CsvREader from LumentWorks. Its free. And it has good features.

Here's a smaple code that i have used :

      CsvReader csv = new CsvReader(textreader, hasHeaders, delimiter);

      int fieldCount = csv.FieldCount;
      string[] headers = null;
      if (m_bHasCSVHeaders)
        headers = csv.GetFieldHeaders();     

      while (csv.ReadNextRecord())
      {...}

Hope it helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜