解析包含维度不等的子表的csv文件

ylamdve6  于 2022-12-15  发布在  其他
关注(0)|答案(1)|浏览(112)

我有一个包含以下示例数据的csv文件:

[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,,,,,,

我需要帮助来弄清楚如何使用python将其放置到两个单独的表中。到目前为止,我有以下代码,但当我尝试使用它时,我在“if row ['RECORDNAME '] == 'Network Settings':“上得到了一个键错误。

# 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)

如有任何建议,我们将不胜感激。

rsl1atfo

rsl1atfo1#

这是解决这个问题的一种方法,读取一个表的文件,然后存储那个数据。注解添加到代码中,希望它们有帮助。

# 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

可以像result['Network Settings']一样访问每个表。

相关问题