python - Having trouble parsing a .CSV file into a dict -




i've done simple .csv parsing in python have new file structure that's giving me trouble. input file spreadsheet converted .csv file. here example of input: layout

each set can have many layouts, , each layout can have many layers. each layer has 1 layer , name.

here code using parse in. suspect it's logic/flow control problem because i've parsed things in before, not deep. first header row skipped via code. appreciated!

import csv import pprint  def import_layouts_schema(layouts_schema_file_name = 'c:\\layouts\\layout1.csv'):      class set_template:      def __init__(self):          self.set_name    =''         self.layout_name =''         self.layer_name  =''         self.obj_name    =''       def check_layout(st, row, layouts_schema):          c=0          if st.layout_name == '':              st.layer_name = row[c+2]             st.obj_name = row[c+3]              layer = {st.layer_name : st.obj_name}              layout = {st.layout_name : layer}             layouts_schema.update({st.set_name : layout})          else:              st.layout_name = row[c+1]             st.layer_name = row[c+2]             st.obj_name = row[c+3]              layer = {st.layer_name : st.obj_name}              layout = {st.layout_name : layer}              layouts_schema.update({st.set_name : layout})          return layouts_schema       def layouts_schema_parsing(obj_list_raw1): #, location_categories, image_schema, set_location):          #------ init -----------------------------------          skipfirst = true          c = 0          firstrow = true          layouts_schema = {}          end_flag = ''          st = set_template()          #---------- start parsing here -----------------          print('now parsing layouts schema list')          row in obj_list_raw1:               #print ('this row: ', row)              if skipfirst==true:                  skipfirst=false                  continue              if row[c] != '':                  st.set_name = row[c]                  st.layout_name = row[c+1]                  st.layer_name = row[c+2]                  st.obj_name = row[c+3]                   print('found new set.  set details below:')                  print('set name:', st.set_name, 'layout name:', st.layout_name, 'layer name:', st.layer_name, 'object name:', st.obj_name)                  if firstrow == true:                      print('first row of layouts import!')                      layer = {st.layer_name : st.obj_name}                      layout = {st.layout_name : layer}                      layouts_schema = {st.set_name : layout}                    firstrow = false                  check_layout(st, row, layouts_schema)                  continue             elif firstrow == false:                 print('not first row of layout import')                  layer = {st.layer_name : st.obj_name}                 layout = {st.layout_name : layer}                 layouts_schema.update({st.set_name : layout})                   check_layout(st, row, layouts_schema)          return layouts_schema       #begin subroutine main      layouts_schema_file_name ='c:\\users\\jason\\documents\\ray\\layout_schemas\\anibot_layouts_schema.csv'      full_path_to_file = layouts_schema_file_name       print('============ importing layouts schema from: ', full_path_to_file , ' ==============')      openfile = open(full_path_to_file)      reader_ob = csv.reader(openfile)      layout_list_raw1 = list(reader_ob)      layouts_schema = layouts_schema_parsing(layout_list_raw1)      print('=========== end of layouts schema import =========')       return layouts_schema     layouts_schema = import_layouts_schema() 

feel free throw part away doesn't work. suspect i've inside head little bit here. loop or while loop may trick. want parse file dict same key structure shown. i.e. final dict's first line like:

{'restaurant': {'rr_facing1': {'backdrop': 'restaurant1'}}}

and rest on there. goign use key structure , dict other purposes. can't parsing down!

wouaw, that's lot of code !

maybe try simpler :

with open('file.csv') f:     keys = f.readline().split(';') # assuming ";" csv fields separator     line in f:         vals = line.split(';')         d = dict(zip(keys, vals))         print(d) 

then either make better data file (without blanks), or have parser remembering previous values.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -