C
C#10mo ago
Zomboos

❔ Csv textfieldparser

csv:
name;horsepower;type
qford;300;car
eford;200;car
name;horsepower;type
qford;300;car
eford;200;car
i can read one line at a time but how would i got about getting the values of that line (for example i want just qford or just 300 or just the type)
11 Replies
Pobiega
Pobiega10mo ago
split the string on ;? are you making a csv parser for fun/learning?
Zomboos
Zomboos10mo ago
yes
Pobiega
Pobiega10mo ago
if not, consider using CsvHelper k
Zomboos
Zomboos10mo ago
if it helps heres what i have right now

using (TextFieldParser parser = new TextFieldParser(@"C:\Users\SemvanHaaften\source\repos\Vehicles\CollectionClasses\cars.csv"))
{
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(";");
while (!parser.EndOfData)
{
//Processing row
string[] fields = parser.ReadFields();
Console.WriteLine(parser.ReadLine());


Console.WriteLine();
}
}

using (TextFieldParser parser = new TextFieldParser(@"C:\Users\SemvanHaaften\source\repos\Vehicles\CollectionClasses\cars.csv"))
{
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(";");
while (!parser.EndOfData)
{
//Processing row
string[] fields = parser.ReadFields();
Console.WriteLine(parser.ReadLine());


Console.WriteLine();
}
}
Pobiega
Pobiega10mo ago
okay, and you are trying to write parser.ReadFields();?
Zomboos
Zomboos10mo ago
no that part is something i forgot to remove to be honest i was seeing what fields were and looking at everything parser can do wait i think uve actually helped me
Pobiega
Pobiega10mo ago
uhm.. is TextFieldParser actually Microsoft.VisualBasic.FileIO.TextFieldParser?
Zomboos
Zomboos10mo ago
now i think about it i can split the string into an array with the ; ye
Pobiega
Pobiega10mo ago
oh, okay. I mean, I thought you were writing a csv parser from scratch TFP is essentially feature complete, if a bit more basic than CsvHelper ReadFields should handle the splitting for you remember that string.Split returns string[] just like ReadFields does
Zomboos
Zomboos10mo ago
alright thanks that will help me on my way ye so reading the line and then reading the fields worked thanks
Accord
Accord10mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.