Solved

How to read columns of a tab delimited text file.

Posted on 2012-03-16
6
2,728 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
6 Comments
 
LVL 74

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 69

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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 74

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now