parse a data.csv in go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -*- compile-command: "go run *.go sample.csv" -*- | |
package main | |
import ( | |
"bufio" | |
"encoding/json" | |
"fmt" | |
"io" | |
"os" | |
"strings" | |
) | |
var material_albedo_diffuse = 0 | |
var material_specular_metalness = 0 | |
var material_specular_metalness_off = 0 | |
func process_line(line string) { | |
splits := strings.SplitN(line, ",", 2) | |
modelID := splits[0] | |
modelID = modelID[1 : len(modelID)-1] | |
jsonString := splits[1] | |
jsonString = strings.Replace(jsonString, "\"\"", "\"", -1) | |
jsonString = jsonString[1 : len(jsonString)-2] | |
byt := []byte(jsonString) | |
var jsonStruct map[string]interface{} | |
if err := json.Unmarshal(byt, &jsonStruct); err != nil { | |
panic(err) | |
} | |
material_diffuse := false | |
material_albedo := false | |
material_specular := false | |
material_metalness := false | |
delete(jsonStruct, "updatedAt") | |
for _, v := range jsonStruct { | |
materials := v.(map[string]interface{}) | |
channels := materials["channels"].(map[string]interface{}) | |
diffuse_pbr := channels["DiffusePBR"].(map[string]interface{}) | |
albedo_pbr := channels["AlbedoPBR"].(map[string]interface{}) | |
metalness_pbr := channels["MetalnessPBR"].(map[string]interface{}) | |
specular_pbr := channels["SpecularPBR"].(map[string]interface{}) | |
if diffuse_pbr["enable"].(bool) { | |
material_diffuse = true | |
} | |
if albedo_pbr["enable"].(bool) { | |
material_albedo = true | |
} | |
metal_enable := metalness_pbr["enable"].(bool) | |
if metal_enable { | |
material_metalness = true | |
} | |
spec_enable := specular_pbr["enable"].(bool) | |
if spec_enable { | |
material_specular = true | |
} | |
if !metal_enable && !spec_enable { | |
material_specular_metalness_off += 1 | |
fmt.Println(modelID) | |
break | |
} | |
if material_albedo && material_diffuse { | |
material_albedo_diffuse += 1 | |
fmt.Println(modelID) | |
break | |
} | |
if material_metalness && material_specular { | |
material_specular_metalness += 1 | |
fmt.Println(modelID) | |
break | |
} | |
} | |
} | |
func readFileWithReadString(fn string) (err error) { | |
file, err := os.Open(fn) | |
if err != nil { | |
return err | |
} | |
// Start reading from the file with a reader. | |
reader := bufio.NewReader(file) | |
var line string | |
// strip first line | |
line, err = reader.ReadString('\n') | |
for { | |
line, err = reader.ReadString('\n') | |
if len(line) > 0 { | |
go process_line(line) | |
} | |
if err != nil { | |
break | |
} | |
} | |
fmt.Printf("case specular/metalness %d, albedo/diffuse %d, specular/metalness off %d\n", material_specular_metalness, material_albedo_diffuse, material_specular_metalness_off) | |
if err != io.EOF { | |
fmt.Printf(" > Failed!: %v\n", err) | |
} | |
defer file.Close() | |
return | |
} | |
func main() { | |
// https://stackoverflow.com/questions/1821811/how-to-read-write-from-to-file-using-go#9739903 | |
// read the whole file at once | |
if len(os.Args) > 1 { | |
filename := os.Args[1] | |
readFileWithReadString(filename) | |
} else { | |
fmt.Println("usage " + os.Args[0] + " filename.csv") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment