C
C#3mo ago
SCOLANATOR

How to add additional headers/columns to CSV file?

Using CSVHelper to manipulate a CSV file but when I try to add additional columns of information I get the error, I thought this would be really simple to add. Error: Header with name 'Payee_Country'[0] was not found. Header with name 'Payer_Address1'[0] was not found. Header with name 'Payer_Address2'[0] was not found. Header with name 'Payer_City'[0] was not found. Header with name 'Payer_State' [0] was not found. Header with name 'Payer_Zip'[0] was not found. Header with name 'Payer_Country'[0] was not found.
9 Replies
Pobiega
Pobiega3mo ago
CSVHelper doesnt by default allow missing headers but if you create a config and set it up correctly, it allows it just fine
var config = new CsvConfiguration(CultureInfo.InvariantCulture) { HeaderValidated = null, MissingFieldFound = null };

using (var reader = new StreamReader("data.csv"))
using (var csv = new CsvReader(reader, config))
{
list = csv.GetRecords<T>().ToList();
}
var config = new CsvConfiguration(CultureInfo.InvariantCulture) { HeaderValidated = null, MissingFieldFound = null };

using (var reader = new StreamReader("data.csv"))
using (var csv = new CsvReader(reader, config))
{
list = csv.GetRecords<T>().ToList();
}
note how I set both HeaderValidated and MissingFieldFound to null here this allows me to parse a file that doesnt match my record class. HOWEVER this is a bad idea If you know the CSV file doesnt contain these values and you want to add them, you should have two models one without and one with the new fields then simply read the values, then use .Select or some other method to turn your objects from A into B, adding the data as needed, then save the list of Bs
SCOLANATOR
SCOLANATOR3mo ago
Could you show me how to do that? I'm struggling a bit here
Pobiega
Pobiega3mo ago
List<OriginalRecord> list;

var config = new CsvConfiguration(CultureInfo.InvariantCulture) { HeaderValidated = null, MissingFieldFound = null };

using (var reader = new StreamReader("data.csv"))
using (var csv = new CsvReader(reader, config))
{
list = csv.GetRecords<OriginalRecord>().ToList();
}

var newList = list.Select(x => new UpdatedRecord()
{
Id = x.Id, Name = x.Name, Time = x.Time, RandomNumber = Random.Shared.Next(100),
}).ToList();

// newList now contains the updated list
List<OriginalRecord> list;

var config = new CsvConfiguration(CultureInfo.InvariantCulture) { HeaderValidated = null, MissingFieldFound = null };

using (var reader = new StreamReader("data.csv"))
using (var csv = new CsvReader(reader, config))
{
list = csv.GetRecords<OriginalRecord>().ToList();
}

var newList = list.Select(x => new UpdatedRecord()
{
Id = x.Id, Name = x.Name, Time = x.Time, RandomNumber = Random.Shared.Next(100),
}).ToList();

// newList now contains the updated list
SCOLANATOR
SCOLANATOR3mo ago
Thank you, I'm not quite sure how to add this into my code but I'll give it a go!
Pobiega
Pobiega3mo ago
step 1 is to just make a record type that has only the headers and values that are in your current csv file then you make the second record type that has the ones you want in your output then its as simple as making a method that converts from type 1 into type 2, passing that method to .Select (I used a lambda, but you can pass a real method too) and you're pretty much done
SCOLANATOR
SCOLANATOR3mo ago
Could you help me put this into my code? I'm banging my head trying to do this, it's been a while since I was last working on C# and this code
Pobiega
Pobiega3mo ago
This is very basic stuff, except maybe the .Select call but I can help you with that have you made the two models and the mapping method? @SCOLANATOR ?
SCOLANATOR
SCOLANATOR3mo ago
@Pobiega Sorry for the delayed response, had something come up. This was what i've put together so far but there's an issue with line 188
Want results from more Discord servers?
Add your server
More Posts