C
C#15mo 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
Pobiega15mo ago
split the string on ;? are you making a csv parser for fun/learning?
Zomboos
ZomboosOP15mo ago
yes
Pobiega
Pobiega15mo ago
if not, consider using CsvHelper k
Zomboos
ZomboosOP15mo 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
Pobiega15mo ago
okay, and you are trying to write parser.ReadFields();?
Zomboos
ZomboosOP15mo 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
Pobiega15mo ago
uhm.. is TextFieldParser actually Microsoft.VisualBasic.FileIO.TextFieldParser?
Zomboos
ZomboosOP15mo ago
now i think about it i can split the string into an array with the ; ye
Pobiega
Pobiega15mo 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
ZomboosOP15mo ago
alright thanks that will help me on my way ye so reading the line and then reading the fields worked thanks
Accord
Accord15mo 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.
Want results from more Discord servers?
Add your server