anyone knows how to pull data from CSV file that uploaded as multipart.file based on its column and then input the data to another CSV file (the template)?
I still can't get the right logic to do that in golang. this is the csv file look alike:
"Coorporate",,,
Address :,,,
Boulevard Street,,,
Service / Office : 021-62318113 / 021-22684901,,,
"VGA WITH GUARANTEE",Dealer,Msrp,MARGIN
VGA-1,"37,000","38,000","1,000"
VGA-2,"27,500","28,500","1,000"
CASE ,Dealer ,Msrp ,MARGIN
CASE-1 ,"430,000","480,000","50,000"
CASE-2 ,"450,000","500,000","50,000"
"MAXSUN VGA ",Dealer ,Msrp ,MARGIN
MVGA-1,"13,300,000","13,800,000","500,000"
these are the codes I've tried to read to detect the table header (the row that at least having "Msrp" column):
reader := csv.NewReader(f)
records, err := reader.ReadAll()
var rows []string
columns := []string{"Msrp", "MSRP", "msrp"}
for _, column := range columns {
for _, columnName := range records {
for _, cols := range columnName {
if cols == column {
row := strings.Join(columnName, " ")
rows = append(rows, row)
break
}
}
}
but it just return the table header only. I need to get the value for each column and save it into a new slice (maybe?) so that later I can update the CSV template file with the value that I just pull from the CSV file uploaded
and let's say i have a struct :
type columnName struct {
PartName string json:"part_name"
Price int json:"price"
Msrp int json:"msrp"
}
how do i match the column name to that struct?
is there any better way to solve this problem?
anyway I'm new to golang and programming pls help me
many thanks!
I cleaned up the input CSV you shared:
Looking at it in an aligned table view:
It looks like you have a couple of rows at the top that won't end up in the final data, so I iterate over them and ignore them:
I made my struct like:
Now, I can iterate the rest of the rows and be on guard for intermediate looking headers, if I see one I just drop it by continuing to the next row, which should be real data. For the real data rows the format looks straightforward—
string, string, int, int
—so I can parse the 2nd and 3rd column directly as ints (checking for errors), append the results to my slice of Records, then turn it all into JSON:and I see:
Bad data in one of the number columns gets logged like:
Here's a complete example, Go Playground.