Solved

How to read columns of a tab delimited text file.

Posted on 2012-03-16
6
3,071 Views
Last Modified: 2012-03-16
I need to read each column value of a tab delimited file.
The file is retrieved.
The column values are not returned.
What do I need to modify?
Thanks,

Source file:
Tab delimited

Date      OldExchange      OldSymbol      NewExchange      NewSymbol
20120315      OTCBB            REFGD      OTCBB            REFG
20120315      OTCBB            SRWY      OTCBB            SRWYD
20120309      OTCBB            ALDFF      AMEX            AXX
20120307      OTCBB            SRCPD      NASDAQ            SRCP

I need to get the column values of each column.

str1 = value of first column "20120315";
str2 = value of 2nd column "OTCBB";
etc.

This is what I have now:
 while ((line = sr.ReadLine()) != null)
        {
            values = line.Split(new char[] { ',' });
            values[values.Length - 1] = values[values.Length - 1].Trim(new char[] { '"' });

           
values[0];
//Returns values[0] as "20120315\tOTCBB\tREFGD\tOTCBB\tREFG"

values[1];
//error Index was outside the bounds of the array.

How do I return the actual column values?
0
Comment
Question by:Dovberman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 125 total points
ID: 37730538
Your logic is splitting on a comma, but you say its a tab-delimited file. Have you tried changing the delimiter for your split?

e.g.

...

values = line.Split(new char[] { '\t' });

...

Open in new window

0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 125 total points
ID: 37730690
0
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 125 total points
ID: 37730781
here is another solution which looks like better and fast in parsing files(like csv)
http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader

Just see that you change the csv delimiter with \t.

Here is a simple solution:-
public List<string[]> parseCSV(string path)
{
  List<string[]> parsedData = new List<string[]>();

  try
  {
    using (StreamReader readFile = new StreamReader(path))
    {
      string line;
      string[] row;

      while ((line = readFile.ReadLine()) != null)
      {
        row = line.Split(new char[] { '\t' });
        parsedData.Add(row);
      }
    }
  }
  catch (Exception e)
  {
    throw;
  }

  return parsedData;
}

Open in new window

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 13

Assisted Solution

by:Naman Goel
Naman Goel earned 125 total points
ID: 37730820
Here is code for you used '\t' as deliminator in Spilt method

using System;
using System.IO;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            //read from textfile1.txt
            using (StreamReader sr = new StreamReader("TextFile1.txt"))
            {
                string line = String.Empty;
                string[] values = null;

                while ((line = sr.ReadLine()) != null)
                {
                    values = line.Split(new char[] { '\t' });
                    values[values.Length - 1] = values[values.Length - 1].Trim(new char[] { '"' });
                    // and you will get all columns in values array as expected

                }
            }
        }
    }
}

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37730827
If two more people come in and mention using tab instead of comma for the delimiter, does that mean I get a Yahtzee?
0
 

Author Closing Comment

by:Dovberman
ID: 37731554
The tab delimiter '\t' resolved the issue. Additional detail was also helpful.
Thanks,
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question