Reading from Comma or Tab Delimited Text File

Reading from comma or tab delimited text file

Starting from PHP 5.3, you can use str_getcsv() to read individual lines using different delimiters.

$someCondition = someConditionToDetermineTabOrComma();

$delimiter = $someCondition ? "," : "\t";

$fp = fopen('mydata.csv', 'r');

while ( !feof($fp) )
{
$line = fgets($fp, 2048);

$data = str_getcsv($line, $delimiter);

doSomethingWithData($data);
}

fclose($fp);

Read tab delimited text file

It is an issue about encoding. Please see this thread for more information (Get "embedded nul(s) found in input" when reading a csv using read.csv()).

url <- 'https://onlinecourses.science.psu.edu/stat501/sites/onlinecourses.science.psu.edu.stat501/files/data/leukemia_remission.txt'
df <- read.table(url, sep = '\t',header = TRUE, fileEncoding = "UTF-16LE")

Reading a delimited text file by line and by delimiter in C#

string[] lines = File.ReadAllLines( filename );
foreach ( string line in lines )
{
string[] col = line.Split(',');
// process col[0], col[1], col[2]
}

How to read and write comma delimited text values to file in VB 6.0

(Note: this answer is assuming that the text file only contains one line.)

First, you will need to read the text file:

Dim rawData as string

Dim sFileText as String
Dim FileNo as Integer
FileNo = FreeFile
Open "C:\test.txt" For Input As #FileNo 'you should change the file path
Line Input #FileNo, sFileText 'read the whole line
rawData = sFileText 'store the first line of the text file in 'rawData'
Close #FileNo

Next, you need to split the rawData by the commas:

Dim data() as string 'an array that will hold each value
data = Split(rawData, ",") 'split 'rawData' with a comma as delimiter

Now the first value is stored in data(0), second in data(1), etc.

As far as the 'save file' button, you can do something like:

Dim newData as String
newData = data(0) & "," & data(1) & "," & data(2) 'etc.

Then write it to a file.

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

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.



Related Topics



Leave a reply



Submit