Reading only specific columns from a CSV file out of many
I have a CSV file which looks like this basically:
TransactionID ProfileID Date // more columns here...
somevalue 123123123 somedate
somevalue 123123123 somedate
somevalue 123123123 somedate
somevalue 123123123 somedate
somevalue 123123123 somedate
I would like to extract only specific columns which are ProfileID and Date out of all these columns from the CSV file, either by mapping it to an existing class like this:
public class MyMappedCSVFile
{
public string ProfileID { get; set; }
public string Date { get; set; }
}
Or by storing it in a Dictionary collection?
I have tried something like this:
public static List<string> ReadInCSV(string absolutePath)
{
List<string> result = new List<string>();
string value;
using (TextReader fileReader = File.OpenText(absolutePath))
{
var csv = new CsvReader(fileReader);
csv.Configuration.HasHeaderRecord = false;
while (csv.Read())
{
for (int i = 0; csv.TryGetField<string>(i, out value); i++)
{
result.Add(value);
}
}
}
return result;
}
But this takes out everything out of the CSV file, literally everything in a single List, and it's not what I want...
Can someone help me out with this ?
c# asp.net asp.net-mvc file csv
add a comment |
I have a CSV file which looks like this basically:
TransactionID ProfileID Date // more columns here...
somevalue 123123123 somedate
somevalue 123123123 somedate
somevalue 123123123 somedate
somevalue 123123123 somedate
somevalue 123123123 somedate
I would like to extract only specific columns which are ProfileID and Date out of all these columns from the CSV file, either by mapping it to an existing class like this:
public class MyMappedCSVFile
{
public string ProfileID { get; set; }
public string Date { get; set; }
}
Or by storing it in a Dictionary collection?
I have tried something like this:
public static List<string> ReadInCSV(string absolutePath)
{
List<string> result = new List<string>();
string value;
using (TextReader fileReader = File.OpenText(absolutePath))
{
var csv = new CsvReader(fileReader);
csv.Configuration.HasHeaderRecord = false;
while (csv.Read())
{
for (int i = 0; csv.TryGetField<string>(i, out value); i++)
{
result.Add(value);
}
}
}
return result;
}
But this takes out everything out of the CSV file, literally everything in a single List, and it's not what I want...
Can someone help me out with this ?
c# asp.net asp.net-mvc file csv
1
The code is just doing what you say - extracting every value from the csv file. Shouldn't your list be a list of MyMappedCSVFile, not string, and in the csv.Read loop you get the two values for a single entry from the two relevant columns?
– JohnRC
Oct 25 '17 at 12:54
add a comment |
I have a CSV file which looks like this basically:
TransactionID ProfileID Date // more columns here...
somevalue 123123123 somedate
somevalue 123123123 somedate
somevalue 123123123 somedate
somevalue 123123123 somedate
somevalue 123123123 somedate
I would like to extract only specific columns which are ProfileID and Date out of all these columns from the CSV file, either by mapping it to an existing class like this:
public class MyMappedCSVFile
{
public string ProfileID { get; set; }
public string Date { get; set; }
}
Or by storing it in a Dictionary collection?
I have tried something like this:
public static List<string> ReadInCSV(string absolutePath)
{
List<string> result = new List<string>();
string value;
using (TextReader fileReader = File.OpenText(absolutePath))
{
var csv = new CsvReader(fileReader);
csv.Configuration.HasHeaderRecord = false;
while (csv.Read())
{
for (int i = 0; csv.TryGetField<string>(i, out value); i++)
{
result.Add(value);
}
}
}
return result;
}
But this takes out everything out of the CSV file, literally everything in a single List, and it's not what I want...
Can someone help me out with this ?
c# asp.net asp.net-mvc file csv
I have a CSV file which looks like this basically:
TransactionID ProfileID Date // more columns here...
somevalue 123123123 somedate
somevalue 123123123 somedate
somevalue 123123123 somedate
somevalue 123123123 somedate
somevalue 123123123 somedate
I would like to extract only specific columns which are ProfileID and Date out of all these columns from the CSV file, either by mapping it to an existing class like this:
public class MyMappedCSVFile
{
public string ProfileID { get; set; }
public string Date { get; set; }
}
Or by storing it in a Dictionary collection?
I have tried something like this:
public static List<string> ReadInCSV(string absolutePath)
{
List<string> result = new List<string>();
string value;
using (TextReader fileReader = File.OpenText(absolutePath))
{
var csv = new CsvReader(fileReader);
csv.Configuration.HasHeaderRecord = false;
while (csv.Read())
{
for (int i = 0; csv.TryGetField<string>(i, out value); i++)
{
result.Add(value);
}
}
}
return result;
}
But this takes out everything out of the CSV file, literally everything in a single List, and it's not what I want...
Can someone help me out with this ?
c# asp.net asp.net-mvc file csv
c# asp.net asp.net-mvc file csv
edited Oct 25 '17 at 13:06
Uwe Keim
27.5k32130212
27.5k32130212
asked Oct 25 '17 at 12:46
User987User987
1,33732045
1,33732045
1
The code is just doing what you say - extracting every value from the csv file. Shouldn't your list be a list of MyMappedCSVFile, not string, and in the csv.Read loop you get the two values for a single entry from the two relevant columns?
– JohnRC
Oct 25 '17 at 12:54
add a comment |
1
The code is just doing what you say - extracting every value from the csv file. Shouldn't your list be a list of MyMappedCSVFile, not string, and in the csv.Read loop you get the two values for a single entry from the two relevant columns?
– JohnRC
Oct 25 '17 at 12:54
1
1
The code is just doing what you say - extracting every value from the csv file. Shouldn't your list be a list of MyMappedCSVFile, not string, and in the csv.Read loop you get the two values for a single entry from the two relevant columns?
– JohnRC
Oct 25 '17 at 12:54
The code is just doing what you say - extracting every value from the csv file. Shouldn't your list be a list of MyMappedCSVFile, not string, and in the csv.Read loop you get the two values for a single entry from the two relevant columns?
– JohnRC
Oct 25 '17 at 12:54
add a comment |
3 Answers
3
active
oldest
votes
The header is still to skip in this code, but with this code, you can choose which columns to extract. It might be much faster if you use a StreamReader.
And you will need a constructor for your object.
var temp = File.ReadAllLines(@"C:myFile.csv");
public List<MyMappedCSVFile>() myExtraction = new List<MyMappedCSVFile>();
foreach(string line in temp)
{
var delimitedLine = line.Split('t'); //set ur separator, in this case tab
myExtraction.Add(new MyMappedCSVFile(delimitedLine[0], delimitedLine[3]));
}
Code for your Object:
public class MyMappedCSVFile
{
public string ProfileID { get; set; }
public string Date { get; set; }
public MyMappedCSVFile(string profile, string date)
{
ProfileID = profile;
Date = date;
}
}
okay this looks good but your code requires some modifications for C# =D
– User987
Oct 25 '17 at 13:09
What kind of modifications do you have in mind? This is just basic .Net.
– kurdy
Oct 25 '17 at 13:10
I see you edited the code... I was looking at the first one you posted, you were missing the object creation inside the List when adding items , but yes it's good now =)
– User987
Oct 25 '17 at 13:11
Ye, just wrote it in my head and forgot to use a constructor in the list blush
– kurdy
Oct 25 '17 at 13:12
add a comment |
Hi You may use this code snippets to read any kind of csv file with little customization according to your needs.
public class CsvRow : List<string>
{
public string LineText { get; set; }
}
public class CsvFileReader : StreamReader
{
public CsvFileReader(Stream stream)
: base(stream)
{
}
public CsvFileReader(string filename)
: base(filename)
{
}
public bool ReadRow(CsvRow row,char separator)
{
try
{
row.LineText = ReadLine();
if (String.IsNullOrEmpty(row.LineText))
return false;
int pos = 0;
int rows = 0;
while (pos < row.LineText.Length)
{
string value;
// Special handling for quoted field
if (row.LineText[pos] == '"')
{
// Skip initial quote
pos++;
// Parse quoted value
int start = pos;
while (pos < row.LineText.Length)
{
// Test for quote character
if (row.LineText[pos] == '"')
{
// Found one
pos++;
// If two quotes together, keep one
// Otherwise, indicates end of value
if (pos >= row.LineText.Length || row.LineText[pos] != '"')
{
pos--;
break;
}
}
pos++;
}
value = row.LineText.Substring(start, pos - start);
value = value.Replace("""", """);
}
else
{
// Parse unquoted value
int start = pos;
while (pos < row.LineText.Length && row.LineText[pos] != separator)
pos++;
value = row.LineText.Substring(start, pos - start);
}
// Add field to list
if (rows < row.Count)
row[rows] = value;
else
row.Add(value);
rows++;
// Eat up to and including next comma
while (pos < row.LineText.Length && row.LineText[pos] != separator)
pos++;
if (pos < row.LineText.Length)
pos++;
}
// Delete any unused items
while (row.Count > rows)
row.RemoveAt(rows);
// Return true if any columns read
return (row.Count > 0);
}
catch (Exception ex)
{
ex.ToString();
throw;
}
}
}
Than just call the function as follows
using (CsvFileReader reader = new CsvFileReader(filePath))
{
char separator = ';'; //CSV file separated by (in this case it is semicolon)
List<MyMappedCSVFile> lst=new List<MyMappedCSVFile>();
CsvRow row = new CsvRow();
while (reader.ReadRow(row,separator))
{
if (row[0].Equals("TransactionID")) //to skip header
continue;
else
{
MyMappedCSVFile obj=new MyMappedCSVFile();
obj.ProfileID =row[1]; //Column Index of ProfileID
obj.Date = row[2]; // Column index of Date
lst.Add(obj);
}
}
}
filePath is the path for your csv file
You could easily enhance this code, if you parameterize the delimiter. CSV by default is character, not semicolon separated. Or have I overlooked?
– kurdy
Oct 25 '17 at 13:23
@kurdy yes you are right, I updated the code. it can be refactored and optimized further.
– mzh
Oct 25 '17 at 13:35
add a comment |
CvsHelper is a nice package you can use for this sort of thing, it lets you get by index or name.
An example from the documents is
// Don't forget to read the data before getting it.
csv.Read();
// By position
var field = csv[0];
// By header name
var field = csv["HeaderName"];
The docs give lots of examples of reading the file and iterating over it, and mapping to classes and so on. Seems perfect for this.
https://github.com/JoshClose/CsvHelper
Here is a more complete answer to your solution in realtion to reading (as you have installed the package)
TextReader fileReader = File.OpenText(somepath);
var csv = new CsvReader(fileReader);
var records = csv.GetRecords<MyMappedCSVFile>();
records will be a list of your CSV rows mapped to your object.
Doing a CSV[0] just gives me a column header? Not the values of that column ?
– User987
Oct 25 '17 at 13:01
I have updated the answer to give a code example which I have tested. it returns a list of records based on your MyMappedCSVFile object
– Tom Lawrence
Oct 25 '17 at 13:59
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46932863%2freading-only-specific-columns-from-a-csv-file-out-of-many%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The header is still to skip in this code, but with this code, you can choose which columns to extract. It might be much faster if you use a StreamReader.
And you will need a constructor for your object.
var temp = File.ReadAllLines(@"C:myFile.csv");
public List<MyMappedCSVFile>() myExtraction = new List<MyMappedCSVFile>();
foreach(string line in temp)
{
var delimitedLine = line.Split('t'); //set ur separator, in this case tab
myExtraction.Add(new MyMappedCSVFile(delimitedLine[0], delimitedLine[3]));
}
Code for your Object:
public class MyMappedCSVFile
{
public string ProfileID { get; set; }
public string Date { get; set; }
public MyMappedCSVFile(string profile, string date)
{
ProfileID = profile;
Date = date;
}
}
okay this looks good but your code requires some modifications for C# =D
– User987
Oct 25 '17 at 13:09
What kind of modifications do you have in mind? This is just basic .Net.
– kurdy
Oct 25 '17 at 13:10
I see you edited the code... I was looking at the first one you posted, you were missing the object creation inside the List when adding items , but yes it's good now =)
– User987
Oct 25 '17 at 13:11
Ye, just wrote it in my head and forgot to use a constructor in the list blush
– kurdy
Oct 25 '17 at 13:12
add a comment |
The header is still to skip in this code, but with this code, you can choose which columns to extract. It might be much faster if you use a StreamReader.
And you will need a constructor for your object.
var temp = File.ReadAllLines(@"C:myFile.csv");
public List<MyMappedCSVFile>() myExtraction = new List<MyMappedCSVFile>();
foreach(string line in temp)
{
var delimitedLine = line.Split('t'); //set ur separator, in this case tab
myExtraction.Add(new MyMappedCSVFile(delimitedLine[0], delimitedLine[3]));
}
Code for your Object:
public class MyMappedCSVFile
{
public string ProfileID { get; set; }
public string Date { get; set; }
public MyMappedCSVFile(string profile, string date)
{
ProfileID = profile;
Date = date;
}
}
okay this looks good but your code requires some modifications for C# =D
– User987
Oct 25 '17 at 13:09
What kind of modifications do you have in mind? This is just basic .Net.
– kurdy
Oct 25 '17 at 13:10
I see you edited the code... I was looking at the first one you posted, you were missing the object creation inside the List when adding items , but yes it's good now =)
– User987
Oct 25 '17 at 13:11
Ye, just wrote it in my head and forgot to use a constructor in the list blush
– kurdy
Oct 25 '17 at 13:12
add a comment |
The header is still to skip in this code, but with this code, you can choose which columns to extract. It might be much faster if you use a StreamReader.
And you will need a constructor for your object.
var temp = File.ReadAllLines(@"C:myFile.csv");
public List<MyMappedCSVFile>() myExtraction = new List<MyMappedCSVFile>();
foreach(string line in temp)
{
var delimitedLine = line.Split('t'); //set ur separator, in this case tab
myExtraction.Add(new MyMappedCSVFile(delimitedLine[0], delimitedLine[3]));
}
Code for your Object:
public class MyMappedCSVFile
{
public string ProfileID { get; set; }
public string Date { get; set; }
public MyMappedCSVFile(string profile, string date)
{
ProfileID = profile;
Date = date;
}
}
The header is still to skip in this code, but with this code, you can choose which columns to extract. It might be much faster if you use a StreamReader.
And you will need a constructor for your object.
var temp = File.ReadAllLines(@"C:myFile.csv");
public List<MyMappedCSVFile>() myExtraction = new List<MyMappedCSVFile>();
foreach(string line in temp)
{
var delimitedLine = line.Split('t'); //set ur separator, in this case tab
myExtraction.Add(new MyMappedCSVFile(delimitedLine[0], delimitedLine[3]));
}
Code for your Object:
public class MyMappedCSVFile
{
public string ProfileID { get; set; }
public string Date { get; set; }
public MyMappedCSVFile(string profile, string date)
{
ProfileID = profile;
Date = date;
}
}
edited Oct 25 '17 at 13:09
answered Oct 25 '17 at 13:04
kurdykurdy
277213
277213
okay this looks good but your code requires some modifications for C# =D
– User987
Oct 25 '17 at 13:09
What kind of modifications do you have in mind? This is just basic .Net.
– kurdy
Oct 25 '17 at 13:10
I see you edited the code... I was looking at the first one you posted, you were missing the object creation inside the List when adding items , but yes it's good now =)
– User987
Oct 25 '17 at 13:11
Ye, just wrote it in my head and forgot to use a constructor in the list blush
– kurdy
Oct 25 '17 at 13:12
add a comment |
okay this looks good but your code requires some modifications for C# =D
– User987
Oct 25 '17 at 13:09
What kind of modifications do you have in mind? This is just basic .Net.
– kurdy
Oct 25 '17 at 13:10
I see you edited the code... I was looking at the first one you posted, you were missing the object creation inside the List when adding items , but yes it's good now =)
– User987
Oct 25 '17 at 13:11
Ye, just wrote it in my head and forgot to use a constructor in the list blush
– kurdy
Oct 25 '17 at 13:12
okay this looks good but your code requires some modifications for C# =D
– User987
Oct 25 '17 at 13:09
okay this looks good but your code requires some modifications for C# =D
– User987
Oct 25 '17 at 13:09
What kind of modifications do you have in mind? This is just basic .Net.
– kurdy
Oct 25 '17 at 13:10
What kind of modifications do you have in mind? This is just basic .Net.
– kurdy
Oct 25 '17 at 13:10
I see you edited the code... I was looking at the first one you posted, you were missing the object creation inside the List when adding items , but yes it's good now =)
– User987
Oct 25 '17 at 13:11
I see you edited the code... I was looking at the first one you posted, you were missing the object creation inside the List when adding items , but yes it's good now =)
– User987
Oct 25 '17 at 13:11
Ye, just wrote it in my head and forgot to use a constructor in the list blush
– kurdy
Oct 25 '17 at 13:12
Ye, just wrote it in my head and forgot to use a constructor in the list blush
– kurdy
Oct 25 '17 at 13:12
add a comment |
Hi You may use this code snippets to read any kind of csv file with little customization according to your needs.
public class CsvRow : List<string>
{
public string LineText { get; set; }
}
public class CsvFileReader : StreamReader
{
public CsvFileReader(Stream stream)
: base(stream)
{
}
public CsvFileReader(string filename)
: base(filename)
{
}
public bool ReadRow(CsvRow row,char separator)
{
try
{
row.LineText = ReadLine();
if (String.IsNullOrEmpty(row.LineText))
return false;
int pos = 0;
int rows = 0;
while (pos < row.LineText.Length)
{
string value;
// Special handling for quoted field
if (row.LineText[pos] == '"')
{
// Skip initial quote
pos++;
// Parse quoted value
int start = pos;
while (pos < row.LineText.Length)
{
// Test for quote character
if (row.LineText[pos] == '"')
{
// Found one
pos++;
// If two quotes together, keep one
// Otherwise, indicates end of value
if (pos >= row.LineText.Length || row.LineText[pos] != '"')
{
pos--;
break;
}
}
pos++;
}
value = row.LineText.Substring(start, pos - start);
value = value.Replace("""", """);
}
else
{
// Parse unquoted value
int start = pos;
while (pos < row.LineText.Length && row.LineText[pos] != separator)
pos++;
value = row.LineText.Substring(start, pos - start);
}
// Add field to list
if (rows < row.Count)
row[rows] = value;
else
row.Add(value);
rows++;
// Eat up to and including next comma
while (pos < row.LineText.Length && row.LineText[pos] != separator)
pos++;
if (pos < row.LineText.Length)
pos++;
}
// Delete any unused items
while (row.Count > rows)
row.RemoveAt(rows);
// Return true if any columns read
return (row.Count > 0);
}
catch (Exception ex)
{
ex.ToString();
throw;
}
}
}
Than just call the function as follows
using (CsvFileReader reader = new CsvFileReader(filePath))
{
char separator = ';'; //CSV file separated by (in this case it is semicolon)
List<MyMappedCSVFile> lst=new List<MyMappedCSVFile>();
CsvRow row = new CsvRow();
while (reader.ReadRow(row,separator))
{
if (row[0].Equals("TransactionID")) //to skip header
continue;
else
{
MyMappedCSVFile obj=new MyMappedCSVFile();
obj.ProfileID =row[1]; //Column Index of ProfileID
obj.Date = row[2]; // Column index of Date
lst.Add(obj);
}
}
}
filePath is the path for your csv file
You could easily enhance this code, if you parameterize the delimiter. CSV by default is character, not semicolon separated. Or have I overlooked?
– kurdy
Oct 25 '17 at 13:23
@kurdy yes you are right, I updated the code. it can be refactored and optimized further.
– mzh
Oct 25 '17 at 13:35
add a comment |
Hi You may use this code snippets to read any kind of csv file with little customization according to your needs.
public class CsvRow : List<string>
{
public string LineText { get; set; }
}
public class CsvFileReader : StreamReader
{
public CsvFileReader(Stream stream)
: base(stream)
{
}
public CsvFileReader(string filename)
: base(filename)
{
}
public bool ReadRow(CsvRow row,char separator)
{
try
{
row.LineText = ReadLine();
if (String.IsNullOrEmpty(row.LineText))
return false;
int pos = 0;
int rows = 0;
while (pos < row.LineText.Length)
{
string value;
// Special handling for quoted field
if (row.LineText[pos] == '"')
{
// Skip initial quote
pos++;
// Parse quoted value
int start = pos;
while (pos < row.LineText.Length)
{
// Test for quote character
if (row.LineText[pos] == '"')
{
// Found one
pos++;
// If two quotes together, keep one
// Otherwise, indicates end of value
if (pos >= row.LineText.Length || row.LineText[pos] != '"')
{
pos--;
break;
}
}
pos++;
}
value = row.LineText.Substring(start, pos - start);
value = value.Replace("""", """);
}
else
{
// Parse unquoted value
int start = pos;
while (pos < row.LineText.Length && row.LineText[pos] != separator)
pos++;
value = row.LineText.Substring(start, pos - start);
}
// Add field to list
if (rows < row.Count)
row[rows] = value;
else
row.Add(value);
rows++;
// Eat up to and including next comma
while (pos < row.LineText.Length && row.LineText[pos] != separator)
pos++;
if (pos < row.LineText.Length)
pos++;
}
// Delete any unused items
while (row.Count > rows)
row.RemoveAt(rows);
// Return true if any columns read
return (row.Count > 0);
}
catch (Exception ex)
{
ex.ToString();
throw;
}
}
}
Than just call the function as follows
using (CsvFileReader reader = new CsvFileReader(filePath))
{
char separator = ';'; //CSV file separated by (in this case it is semicolon)
List<MyMappedCSVFile> lst=new List<MyMappedCSVFile>();
CsvRow row = new CsvRow();
while (reader.ReadRow(row,separator))
{
if (row[0].Equals("TransactionID")) //to skip header
continue;
else
{
MyMappedCSVFile obj=new MyMappedCSVFile();
obj.ProfileID =row[1]; //Column Index of ProfileID
obj.Date = row[2]; // Column index of Date
lst.Add(obj);
}
}
}
filePath is the path for your csv file
You could easily enhance this code, if you parameterize the delimiter. CSV by default is character, not semicolon separated. Or have I overlooked?
– kurdy
Oct 25 '17 at 13:23
@kurdy yes you are right, I updated the code. it can be refactored and optimized further.
– mzh
Oct 25 '17 at 13:35
add a comment |
Hi You may use this code snippets to read any kind of csv file with little customization according to your needs.
public class CsvRow : List<string>
{
public string LineText { get; set; }
}
public class CsvFileReader : StreamReader
{
public CsvFileReader(Stream stream)
: base(stream)
{
}
public CsvFileReader(string filename)
: base(filename)
{
}
public bool ReadRow(CsvRow row,char separator)
{
try
{
row.LineText = ReadLine();
if (String.IsNullOrEmpty(row.LineText))
return false;
int pos = 0;
int rows = 0;
while (pos < row.LineText.Length)
{
string value;
// Special handling for quoted field
if (row.LineText[pos] == '"')
{
// Skip initial quote
pos++;
// Parse quoted value
int start = pos;
while (pos < row.LineText.Length)
{
// Test for quote character
if (row.LineText[pos] == '"')
{
// Found one
pos++;
// If two quotes together, keep one
// Otherwise, indicates end of value
if (pos >= row.LineText.Length || row.LineText[pos] != '"')
{
pos--;
break;
}
}
pos++;
}
value = row.LineText.Substring(start, pos - start);
value = value.Replace("""", """);
}
else
{
// Parse unquoted value
int start = pos;
while (pos < row.LineText.Length && row.LineText[pos] != separator)
pos++;
value = row.LineText.Substring(start, pos - start);
}
// Add field to list
if (rows < row.Count)
row[rows] = value;
else
row.Add(value);
rows++;
// Eat up to and including next comma
while (pos < row.LineText.Length && row.LineText[pos] != separator)
pos++;
if (pos < row.LineText.Length)
pos++;
}
// Delete any unused items
while (row.Count > rows)
row.RemoveAt(rows);
// Return true if any columns read
return (row.Count > 0);
}
catch (Exception ex)
{
ex.ToString();
throw;
}
}
}
Than just call the function as follows
using (CsvFileReader reader = new CsvFileReader(filePath))
{
char separator = ';'; //CSV file separated by (in this case it is semicolon)
List<MyMappedCSVFile> lst=new List<MyMappedCSVFile>();
CsvRow row = new CsvRow();
while (reader.ReadRow(row,separator))
{
if (row[0].Equals("TransactionID")) //to skip header
continue;
else
{
MyMappedCSVFile obj=new MyMappedCSVFile();
obj.ProfileID =row[1]; //Column Index of ProfileID
obj.Date = row[2]; // Column index of Date
lst.Add(obj);
}
}
}
filePath is the path for your csv file
Hi You may use this code snippets to read any kind of csv file with little customization according to your needs.
public class CsvRow : List<string>
{
public string LineText { get; set; }
}
public class CsvFileReader : StreamReader
{
public CsvFileReader(Stream stream)
: base(stream)
{
}
public CsvFileReader(string filename)
: base(filename)
{
}
public bool ReadRow(CsvRow row,char separator)
{
try
{
row.LineText = ReadLine();
if (String.IsNullOrEmpty(row.LineText))
return false;
int pos = 0;
int rows = 0;
while (pos < row.LineText.Length)
{
string value;
// Special handling for quoted field
if (row.LineText[pos] == '"')
{
// Skip initial quote
pos++;
// Parse quoted value
int start = pos;
while (pos < row.LineText.Length)
{
// Test for quote character
if (row.LineText[pos] == '"')
{
// Found one
pos++;
// If two quotes together, keep one
// Otherwise, indicates end of value
if (pos >= row.LineText.Length || row.LineText[pos] != '"')
{
pos--;
break;
}
}
pos++;
}
value = row.LineText.Substring(start, pos - start);
value = value.Replace("""", """);
}
else
{
// Parse unquoted value
int start = pos;
while (pos < row.LineText.Length && row.LineText[pos] != separator)
pos++;
value = row.LineText.Substring(start, pos - start);
}
// Add field to list
if (rows < row.Count)
row[rows] = value;
else
row.Add(value);
rows++;
// Eat up to and including next comma
while (pos < row.LineText.Length && row.LineText[pos] != separator)
pos++;
if (pos < row.LineText.Length)
pos++;
}
// Delete any unused items
while (row.Count > rows)
row.RemoveAt(rows);
// Return true if any columns read
return (row.Count > 0);
}
catch (Exception ex)
{
ex.ToString();
throw;
}
}
}
Than just call the function as follows
using (CsvFileReader reader = new CsvFileReader(filePath))
{
char separator = ';'; //CSV file separated by (in this case it is semicolon)
List<MyMappedCSVFile> lst=new List<MyMappedCSVFile>();
CsvRow row = new CsvRow();
while (reader.ReadRow(row,separator))
{
if (row[0].Equals("TransactionID")) //to skip header
continue;
else
{
MyMappedCSVFile obj=new MyMappedCSVFile();
obj.ProfileID =row[1]; //Column Index of ProfileID
obj.Date = row[2]; // Column index of Date
lst.Add(obj);
}
}
}
filePath is the path for your csv file
edited Oct 25 '17 at 13:34
answered Oct 25 '17 at 13:10
mzhmzh
376416
376416
You could easily enhance this code, if you parameterize the delimiter. CSV by default is character, not semicolon separated. Or have I overlooked?
– kurdy
Oct 25 '17 at 13:23
@kurdy yes you are right, I updated the code. it can be refactored and optimized further.
– mzh
Oct 25 '17 at 13:35
add a comment |
You could easily enhance this code, if you parameterize the delimiter. CSV by default is character, not semicolon separated. Or have I overlooked?
– kurdy
Oct 25 '17 at 13:23
@kurdy yes you are right, I updated the code. it can be refactored and optimized further.
– mzh
Oct 25 '17 at 13:35
You could easily enhance this code, if you parameterize the delimiter. CSV by default is character, not semicolon separated. Or have I overlooked?
– kurdy
Oct 25 '17 at 13:23
You could easily enhance this code, if you parameterize the delimiter. CSV by default is character, not semicolon separated. Or have I overlooked?
– kurdy
Oct 25 '17 at 13:23
@kurdy yes you are right, I updated the code. it can be refactored and optimized further.
– mzh
Oct 25 '17 at 13:35
@kurdy yes you are right, I updated the code. it can be refactored and optimized further.
– mzh
Oct 25 '17 at 13:35
add a comment |
CvsHelper is a nice package you can use for this sort of thing, it lets you get by index or name.
An example from the documents is
// Don't forget to read the data before getting it.
csv.Read();
// By position
var field = csv[0];
// By header name
var field = csv["HeaderName"];
The docs give lots of examples of reading the file and iterating over it, and mapping to classes and so on. Seems perfect for this.
https://github.com/JoshClose/CsvHelper
Here is a more complete answer to your solution in realtion to reading (as you have installed the package)
TextReader fileReader = File.OpenText(somepath);
var csv = new CsvReader(fileReader);
var records = csv.GetRecords<MyMappedCSVFile>();
records will be a list of your CSV rows mapped to your object.
Doing a CSV[0] just gives me a column header? Not the values of that column ?
– User987
Oct 25 '17 at 13:01
I have updated the answer to give a code example which I have tested. it returns a list of records based on your MyMappedCSVFile object
– Tom Lawrence
Oct 25 '17 at 13:59
add a comment |
CvsHelper is a nice package you can use for this sort of thing, it lets you get by index or name.
An example from the documents is
// Don't forget to read the data before getting it.
csv.Read();
// By position
var field = csv[0];
// By header name
var field = csv["HeaderName"];
The docs give lots of examples of reading the file and iterating over it, and mapping to classes and so on. Seems perfect for this.
https://github.com/JoshClose/CsvHelper
Here is a more complete answer to your solution in realtion to reading (as you have installed the package)
TextReader fileReader = File.OpenText(somepath);
var csv = new CsvReader(fileReader);
var records = csv.GetRecords<MyMappedCSVFile>();
records will be a list of your CSV rows mapped to your object.
Doing a CSV[0] just gives me a column header? Not the values of that column ?
– User987
Oct 25 '17 at 13:01
I have updated the answer to give a code example which I have tested. it returns a list of records based on your MyMappedCSVFile object
– Tom Lawrence
Oct 25 '17 at 13:59
add a comment |
CvsHelper is a nice package you can use for this sort of thing, it lets you get by index or name.
An example from the documents is
// Don't forget to read the data before getting it.
csv.Read();
// By position
var field = csv[0];
// By header name
var field = csv["HeaderName"];
The docs give lots of examples of reading the file and iterating over it, and mapping to classes and so on. Seems perfect for this.
https://github.com/JoshClose/CsvHelper
Here is a more complete answer to your solution in realtion to reading (as you have installed the package)
TextReader fileReader = File.OpenText(somepath);
var csv = new CsvReader(fileReader);
var records = csv.GetRecords<MyMappedCSVFile>();
records will be a list of your CSV rows mapped to your object.
CvsHelper is a nice package you can use for this sort of thing, it lets you get by index or name.
An example from the documents is
// Don't forget to read the data before getting it.
csv.Read();
// By position
var field = csv[0];
// By header name
var field = csv["HeaderName"];
The docs give lots of examples of reading the file and iterating over it, and mapping to classes and so on. Seems perfect for this.
https://github.com/JoshClose/CsvHelper
Here is a more complete answer to your solution in realtion to reading (as you have installed the package)
TextReader fileReader = File.OpenText(somepath);
var csv = new CsvReader(fileReader);
var records = csv.GetRecords<MyMappedCSVFile>();
records will be a list of your CSV rows mapped to your object.
edited Oct 25 '17 at 13:58
answered Oct 25 '17 at 12:57
Tom LawrenceTom Lawrence
535
535
Doing a CSV[0] just gives me a column header? Not the values of that column ?
– User987
Oct 25 '17 at 13:01
I have updated the answer to give a code example which I have tested. it returns a list of records based on your MyMappedCSVFile object
– Tom Lawrence
Oct 25 '17 at 13:59
add a comment |
Doing a CSV[0] just gives me a column header? Not the values of that column ?
– User987
Oct 25 '17 at 13:01
I have updated the answer to give a code example which I have tested. it returns a list of records based on your MyMappedCSVFile object
– Tom Lawrence
Oct 25 '17 at 13:59
Doing a CSV[0] just gives me a column header? Not the values of that column ?
– User987
Oct 25 '17 at 13:01
Doing a CSV[0] just gives me a column header? Not the values of that column ?
– User987
Oct 25 '17 at 13:01
I have updated the answer to give a code example which I have tested. it returns a list of records based on your MyMappedCSVFile object
– Tom Lawrence
Oct 25 '17 at 13:59
I have updated the answer to give a code example which I have tested. it returns a list of records based on your MyMappedCSVFile object
– Tom Lawrence
Oct 25 '17 at 13:59
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46932863%2freading-only-specific-columns-from-a-csv-file-out-of-many%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
The code is just doing what you say - extracting every value from the csv file. Shouldn't your list be a list of MyMappedCSVFile, not string, and in the csv.Read loop you get the two values for a single entry from the two relevant columns?
– JohnRC
Oct 25 '17 at 12:54