Import a CSV and assign items in row to an array c#












-3














I have a CSV which I want to import to a c# console application.
I want to create arrays from each row of the CSV so I end up with a bunch of arrays, like the ones shown below (but would need to ignore the fisrt row if possible as it's a row of headings):



Array1 - [row2column1] [row2column2] [row2column3]
Array2 - [row3column1] [row3column2] [row3column3]
Array3 - [row4column1] [row4column2] [row4column3]


etc...



Is this possible? if so, how would i do this?



Thanks for the help










share|improve this question
























  • How about one two-dimensional array or a List of a class where the properties are the columns expected here or read the CSV into a datatable. Creating N number of arrays dynamically based on the number of incoming lines doesn't sound like a good idea. Besides that, you just loop the file, split the line by whatever delimiter and load to whatever object (list of an object, multi-dim array, etc).
    – JNevill
    Nov 20 at 21:36












  • What type of data, does this build a business entity?
    – Greg
    Nov 20 at 21:39






  • 1




    There are too many 3rd party .NET CSV parsers to count. Just pick any of those that gives you acceptable perforamnce and results. For hte underlying datastructure, the options are jagged or 2 dimensional arrays. Personally I dislike the 2-dimensional style and always go for jagged. docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/… | docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/…
    – Christopher
    Nov 20 at 21:40
















-3














I have a CSV which I want to import to a c# console application.
I want to create arrays from each row of the CSV so I end up with a bunch of arrays, like the ones shown below (but would need to ignore the fisrt row if possible as it's a row of headings):



Array1 - [row2column1] [row2column2] [row2column3]
Array2 - [row3column1] [row3column2] [row3column3]
Array3 - [row4column1] [row4column2] [row4column3]


etc...



Is this possible? if so, how would i do this?



Thanks for the help










share|improve this question
























  • How about one two-dimensional array or a List of a class where the properties are the columns expected here or read the CSV into a datatable. Creating N number of arrays dynamically based on the number of incoming lines doesn't sound like a good idea. Besides that, you just loop the file, split the line by whatever delimiter and load to whatever object (list of an object, multi-dim array, etc).
    – JNevill
    Nov 20 at 21:36












  • What type of data, does this build a business entity?
    – Greg
    Nov 20 at 21:39






  • 1




    There are too many 3rd party .NET CSV parsers to count. Just pick any of those that gives you acceptable perforamnce and results. For hte underlying datastructure, the options are jagged or 2 dimensional arrays. Personally I dislike the 2-dimensional style and always go for jagged. docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/… | docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/…
    – Christopher
    Nov 20 at 21:40














-3












-3








-3







I have a CSV which I want to import to a c# console application.
I want to create arrays from each row of the CSV so I end up with a bunch of arrays, like the ones shown below (but would need to ignore the fisrt row if possible as it's a row of headings):



Array1 - [row2column1] [row2column2] [row2column3]
Array2 - [row3column1] [row3column2] [row3column3]
Array3 - [row4column1] [row4column2] [row4column3]


etc...



Is this possible? if so, how would i do this?



Thanks for the help










share|improve this question















I have a CSV which I want to import to a c# console application.
I want to create arrays from each row of the CSV so I end up with a bunch of arrays, like the ones shown below (but would need to ignore the fisrt row if possible as it's a row of headings):



Array1 - [row2column1] [row2column2] [row2column3]
Array2 - [row3column1] [row3column2] [row3column3]
Array3 - [row4column1] [row4column2] [row4column3]


etc...



Is this possible? if so, how would i do this?



Thanks for the help







c# arrays csv






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 21:34









JNevill

31.3k31544




31.3k31544










asked Nov 20 at 21:33









Dylan

11




11












  • How about one two-dimensional array or a List of a class where the properties are the columns expected here or read the CSV into a datatable. Creating N number of arrays dynamically based on the number of incoming lines doesn't sound like a good idea. Besides that, you just loop the file, split the line by whatever delimiter and load to whatever object (list of an object, multi-dim array, etc).
    – JNevill
    Nov 20 at 21:36












  • What type of data, does this build a business entity?
    – Greg
    Nov 20 at 21:39






  • 1




    There are too many 3rd party .NET CSV parsers to count. Just pick any of those that gives you acceptable perforamnce and results. For hte underlying datastructure, the options are jagged or 2 dimensional arrays. Personally I dislike the 2-dimensional style and always go for jagged. docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/… | docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/…
    – Christopher
    Nov 20 at 21:40


















  • How about one two-dimensional array or a List of a class where the properties are the columns expected here or read the CSV into a datatable. Creating N number of arrays dynamically based on the number of incoming lines doesn't sound like a good idea. Besides that, you just loop the file, split the line by whatever delimiter and load to whatever object (list of an object, multi-dim array, etc).
    – JNevill
    Nov 20 at 21:36












  • What type of data, does this build a business entity?
    – Greg
    Nov 20 at 21:39






  • 1




    There are too many 3rd party .NET CSV parsers to count. Just pick any of those that gives you acceptable perforamnce and results. For hte underlying datastructure, the options are jagged or 2 dimensional arrays. Personally I dislike the 2-dimensional style and always go for jagged. docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/… | docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/…
    – Christopher
    Nov 20 at 21:40
















How about one two-dimensional array or a List of a class where the properties are the columns expected here or read the CSV into a datatable. Creating N number of arrays dynamically based on the number of incoming lines doesn't sound like a good idea. Besides that, you just loop the file, split the line by whatever delimiter and load to whatever object (list of an object, multi-dim array, etc).
– JNevill
Nov 20 at 21:36






How about one two-dimensional array or a List of a class where the properties are the columns expected here or read the CSV into a datatable. Creating N number of arrays dynamically based on the number of incoming lines doesn't sound like a good idea. Besides that, you just loop the file, split the line by whatever delimiter and load to whatever object (list of an object, multi-dim array, etc).
– JNevill
Nov 20 at 21:36














What type of data, does this build a business entity?
– Greg
Nov 20 at 21:39




What type of data, does this build a business entity?
– Greg
Nov 20 at 21:39




1




1




There are too many 3rd party .NET CSV parsers to count. Just pick any of those that gives you acceptable perforamnce and results. For hte underlying datastructure, the options are jagged or 2 dimensional arrays. Personally I dislike the 2-dimensional style and always go for jagged. docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/… | docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/…
– Christopher
Nov 20 at 21:40




There are too many 3rd party .NET CSV parsers to count. Just pick any of those that gives you acceptable perforamnce and results. For hte underlying datastructure, the options are jagged or 2 dimensional arrays. Personally I dislike the 2-dimensional style and always go for jagged. docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/… | docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/…
– Christopher
Nov 20 at 21:40












1 Answer
1






active

oldest

votes


















2














You could use TextFieldParser. Each time you call ReadFields() it returns the next row of data as a string array. Do as you want with each array..



var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(@"C:temptest.csv");
parser.SetDelimiters(",");
parser.ReadFields(); // discard first row
while (!parser.EndOfData)
{
var array = parser.ReadFields(); // next row returned as an array of strings
}





share|improve this answer





















  • Nice, I did not know about this. Just include the Microsoft.VisualBasic.dll. Also see stackoverflow.com/questions/22297562/…
    – Carlo Bos
    Nov 20 at 22:30











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401902%2fimport-a-csv-and-assign-items-in-row-to-an-array-c-sharp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You could use TextFieldParser. Each time you call ReadFields() it returns the next row of data as a string array. Do as you want with each array..



var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(@"C:temptest.csv");
parser.SetDelimiters(",");
parser.ReadFields(); // discard first row
while (!parser.EndOfData)
{
var array = parser.ReadFields(); // next row returned as an array of strings
}





share|improve this answer





















  • Nice, I did not know about this. Just include the Microsoft.VisualBasic.dll. Also see stackoverflow.com/questions/22297562/…
    – Carlo Bos
    Nov 20 at 22:30
















2














You could use TextFieldParser. Each time you call ReadFields() it returns the next row of data as a string array. Do as you want with each array..



var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(@"C:temptest.csv");
parser.SetDelimiters(",");
parser.ReadFields(); // discard first row
while (!parser.EndOfData)
{
var array = parser.ReadFields(); // next row returned as an array of strings
}





share|improve this answer





















  • Nice, I did not know about this. Just include the Microsoft.VisualBasic.dll. Also see stackoverflow.com/questions/22297562/…
    – Carlo Bos
    Nov 20 at 22:30














2












2








2






You could use TextFieldParser. Each time you call ReadFields() it returns the next row of data as a string array. Do as you want with each array..



var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(@"C:temptest.csv");
parser.SetDelimiters(",");
parser.ReadFields(); // discard first row
while (!parser.EndOfData)
{
var array = parser.ReadFields(); // next row returned as an array of strings
}





share|improve this answer












You could use TextFieldParser. Each time you call ReadFields() it returns the next row of data as a string array. Do as you want with each array..



var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(@"C:temptest.csv");
parser.SetDelimiters(",");
parser.ReadFields(); // discard first row
while (!parser.EndOfData)
{
var array = parser.ReadFields(); // next row returned as an array of strings
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 22:05









einar

561




561












  • Nice, I did not know about this. Just include the Microsoft.VisualBasic.dll. Also see stackoverflow.com/questions/22297562/…
    – Carlo Bos
    Nov 20 at 22:30


















  • Nice, I did not know about this. Just include the Microsoft.VisualBasic.dll. Also see stackoverflow.com/questions/22297562/…
    – Carlo Bos
    Nov 20 at 22:30
















Nice, I did not know about this. Just include the Microsoft.VisualBasic.dll. Also see stackoverflow.com/questions/22297562/…
– Carlo Bos
Nov 20 at 22:30




Nice, I did not know about this. Just include the Microsoft.VisualBasic.dll. Also see stackoverflow.com/questions/22297562/…
– Carlo Bos
Nov 20 at 22:30


















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401902%2fimport-a-csv-and-assign-items-in-row-to-an-array-c-sharp%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Feedback on college project

Futebolista

Albești (Vaslui)