Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to read a tab-delimited text file in C#

We have a text file with about 100,000 rows, about 50 columns per row, most of the data is pretty small (5 to 10 characters or numbers).

This is a pretty simple task, but just wondering what the best way would be to import this data into a C# data structure (for example a DataTable)?

like image 610
alchemical Avatar asked Jan 26 '10 22:01

alchemical


People also ask

Is tab-delimited the same as CSV?

A CSV (Comma Separated Values) or Tab-delimited Text (or Tab Separated Values) file is a text file in which one can identify rows and columns. Rows are represented by the lines in the file and the columns are created by separating the values on each line by a specific character, like a comma or a tab.

What is the most common delimiter used in delimited text files?

Delimited formats Any character may be used to separate the values, but the most common delimiters are the comma, tab, and colon. The vertical bar (also referred to as pipe) and space are also sometimes used.

What is a tab-delimited text file?

A tab-delimited file contains rows of data. Each row of data contains one or more pieces of data. Each piece of data is called a field. Tab-delimited files to be read by the Data Integrator must contain the same number of fields in every row, although not every field necessarily needs a value.


1 Answers

I would read it in as a CSV with the tab column delimiters:

A Fast CSV Reader

Edit:
Here's a barebones example of what you'd need:

DataTable dt = new DataTable();
using (CsvReader csv = new CsvReader(new StreamReader(CSV_FULLNAME), false, '\t')) {
    dt.Load(csv);
}

Where CSV_FULLNAME is the full path + filename of your tab delimited CSV.

like image 152
Jay Riggs Avatar answered Jan 05 '23 03:01

Jay Riggs