Created
February 13, 2018 13:19
-
-
Save cedricpinson/4233f21ffd5f32cfb3f331bd2e12c3e8 to your computer and use it in GitHub Desktop.
Parse csv data in python
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
#!/usr/local/bin/python | |
import json | |
import sys | |
import time | |
material_specular_metalness = 0 | |
material_albedo_diffuse = 0 | |
material_specular_metalness_off = 0 | |
def process_line(line): | |
fields = line.split(',', 1 ) | |
model_id = fields[0] | |
materials = fields[1].replace('\"\"','\"') | |
materials_cleaned = materials[1:-2] | |
materials_json = json.loads(materials_cleaned) | |
del materials_json['updatedAt'] # remove this field from the dict | |
material_albedo = False | |
material_diffuse = False | |
material_metalness = False | |
material_specular = False | |
global material_specular_metalness | |
global material_albedo_diffuse | |
global material_specular_metalness_off | |
for material_id, mat in materials_json.iteritems(): | |
channels = mat['channels'] | |
diffuse_pbr = channels['DiffusePBR'] | |
albedo_pbr = channels['AlbedoPBR'] | |
metalness_pbr = channels['MetalnessPBR'] | |
specular_pbr = channels['SpecularPBR'] | |
if diffuse_pbr['enable']: | |
material_diffuse = True | |
if albedo_pbr['enable']: | |
material_albedo = True | |
if metalness_pbr['enable']: | |
material_metalness = True | |
if specular_pbr['enable']: | |
material_specular = True | |
if metalness_pbr is False and specular_pbr is False: | |
material_specular_metalness_off += 1 | |
return model_id | |
if material_albedo and material_diffuse: | |
material_albedo_diffuse += 1 | |
return model_id | |
if material_metalness and material_specular: | |
material_specular_metalness += 1 | |
return model_id | |
return None | |
def parse(filename): | |
first_line = True | |
global material_specular_metalness | |
global material_albedo_diffuse | |
global material_specular_metalness_off | |
with open(filename,"r") as file: | |
for line in file: | |
if first_line: | |
first_line = False | |
continue | |
modelid = process_line(line) | |
if modelid is not None: | |
print modelid | |
print 'case specular/metalness {}, albedo/diffuse {}, specular/metalness off {}'.format(material_specular_metalness,material_albedo_diffuse,material_specular_metalness_off) | |
if __name__ == "__main__": | |
if len(sys.argv) > 1: | |
parse(sys.argv[1]) | |
else: | |
print 'usage: {} data.cvs'.format(sys.argv[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment