Home > Software engineering >  Parse csv file that has subtables of unequal dimensions
Parse csv file that has subtables of unequal dimensions

Time:12-13

I Have a csv file with the following sample data:

[Network]
Network Settings
RECORDNAME,DATA
UTDFVERSION,8
Metric,0
yellowTime,3.5
allRedTime,1.0
Walk,7.0
DontWalk,11.0
HV,0.02
PHF,0.92

[Nodes]
Node Data
INTID,TYPE,X,Y,Z,DESCRIPTION,CBD,Inside Radius,Outside Radius,Roundabout Lanes,Circle Speed
1,1,111152,12379,0,,,,,,
2,1,134346,12311,0,,,,,,
3,3,133315,12317,0,,,,,,
4,1,133284,13574,0,,,,,,

I need help figuring out how to place this into two separate tables using python. I have the following code so far, but I get a keyerror on "if row['RECORDNAME'] == 'Network Settings':" when I try to use it.

# Open the file
with open('filename.csv', 'r') as f:
  # Create a reader
  reader = csv.DictReader(f)
  
  # Initialize empty lists for the tables
  network_table = []
  nodes_table = []
  
  # Loop through the rows
  for row in reader:
    # Check if the row contains the "RECORDNAME" key
    if 'RECORDNAME' in row:
      # Check if the row belongs to the "Network" or "Nodes" section
      if row['RECORDNAME'] == 'Network Settings':
        # Add the row to the "Network" table
        network_table.append(row)
      elif row['INTID'] is not None:
        # Add the row to the "Nodes" table
        nodes_table.append(row)
      
  # Print the tables
  print(network_table)
  print(nodes_table)

Any suggestions would be appreciated.

CodePudding user response:

this is one way to solve this, read the file for one table, and then store that data. comment added to code, hope they helps.

# Gist - read the rows, and determine when the data starts for the [![enter image description here][1]][1]next category
import csv
import re
result={} # store in the list , each item as one type of data
with open('node.csv', 'r') as csvfile:
    csvreader = csv.reader(csvfile, delimiter=',')
    for e in csvreader:
        if re.search('\[.*\]',''.join(e)): # config found pattern [....] example [Network]
            table_data=True
            key_dict=next(csvreader) # get the key, so know the following data below to that category
            key_dict=''.join(key_dict)
            key_data=[]
            while table_data:
                try:
                    detail_data=next(csvreader)
                except:
                    table_data=False # handling the eof
                if not ''.join(detail_data):
                    table_data=False
                key_data.append(detail_data)
            result[key_dict]=key_data # store the data

each table can be accessed like result['Network Settings'].

enter image description here

  • Related