从json文件中提取信息(Ekahau)

问题描述 投票:0回答:1

我正在进行我的第一个 Python 项目,想要从 Ekahau(无线工具)中提取信息,该工具在提取时以 json 格式存储所有信息。 我最初的想法是读取文件中的所有行,在“:”处分隔并将第一部分存储为键,将第二部分存储为字典中的值(到目前为止没有问题)。

我的问题是构建循环,将第一个 AP 的信息存储到一个字典中,一旦下一个键重复(从位置开始),就将值写入第二个字典。

这是否是存储此信息的最佳方式,还是我错过了另一个选择? 我想坚持使用通用代码,因为这也应该扩展到具有类似结构的其他文件。

这是文件结构:

{
  "accessPoints": [
    {
      "location": {
        "floorPlanId": "0f422470-9c8f-41d7-a51f-a19dcd1208cc",
        "coord": {
          "x": 815.4422419685577,
          "y": 636.5659603554341
        }
      },
      "name": "WarehouseAP-4",
      "mine": true,
      "hidden": false,
      "userDefinedPosition": true,
      "noteIds": [],
      "vendor": "Aruba",
      "model": "AP-574 + ANT-2x2-2314 + ANT-4x4-5314",
      "tags": [],
      "id": "f27feef1-3800-4853-a0fe-21bc1da00d2f",
      "status": "CREATED"
    },
    {
      "location": {
        "floorPlanId": "0f422470-9c8f-41d7-a51f-a19dcd1208cc",
        "coord": {
          "x": 2710.3321941216677,
          "y": 981.6322624743677
        }
      },
      "name": "WarehouseAP-3",
      "mine": true,
      "hidden": false,
      "userDefinedPosition": true,
      "noteIds": [],
      "vendor": "Aruba",
      "model": "AP-574 + ANT-2x2-2314 + ANT-4x4-5314",
      "tags": [],
      "id": "535d8800-b264-4737-a4ba-1c69c518ad06",
      "status": "CREATED"
    },

这是我的代码:

        for file_name in all_json_files:  # loop for all json files
            if file_name.endswith(extension):  
                category = file_name[:-5]      
                json_file_path = temp_files + '/' + file_name
                open_file = open(json_file_path, 'r')
                skip_lines = 2  # amount of lines to skip
                current_line = 0    # reset current_line in each file
                list_of_keys = []  # leere list_of_keys für jede neue category/file
                list_of_values = []

                for lines in open_file:  # loop for each line in the json file
                    lines.strip('{')
                    lines.strip('}')
                    if current_line >= skip_lines and len(lines.split(':')) > 1:  
                        line_content = lines.split(':')  
                        key = line_content[0].strip() 
                        value = line_content[1].strip()  
                        value = value[:-1]  # remove comma at end of line
                        if key not in list_of_keys and value != '':
                            list_of_keys.append(key)
                            if value not in list_of_values:
                                list_of_values.append(value)
                            # for key, value in zip(list_of_keys, list_of_values):   # create database
                            #     database[key] = value
                    current_line += 1
                open_file.close()

我期望每个文件都有一个字典,其中内部字典包含有关其中每个项目的信息,以便我可以使用有关导出和进一步计算的信息。

类似这样的东西如果对我有用的话:

accessPoints = {
    'AP001': {'floorPlanId':'', 'name':'Measured-AP-02:f5', 'mine':'true'},
    'AP002': {'floorPlanId':'Warehouse', 'name':'Measured-AP-cc:f8', 'mine':'true'}
}
python json python-3.x loops dictionary
1个回答
0
投票

评论者已经注意到了这一点,但是手动解析 json 是很困难的方法。只需使用

json.load()
,您就可以与字典格式的数据进行交互(这是您的目标)。

顺便说一句 - 如果您选择循环字典,请确保使用

for key, item in dict.items()

否则您只会获得没有数据的键。

© www.soinside.com 2019 - 2024. All rights reserved.