简体   繁体   English

读取 config.txt python 元组格式

[英]read a config.txt python tuple format

Could someone give me a tip on how to read a config.txt in a tuple format?有人可以给我一个关于如何以元组格式读取 config.txt 的提示吗?

config file:配置文件:

# format : key = name, v = (trunk, hardware address) tuple
jci_vavs = {
    'vav1_1': ('11:14'),
    'vav1_2': ('11:13'),
    'vav1_3': ('11:15'),
    'vav1_4': ('11:11'),
    'vav1_5': ('11:9'),
    'vav1_6': ('11:8'),
    'vav1_7': ('11:7'),
    'vav1_8': ('11:6'),
    'vav1_9': ('11:9'),
    'vav1_10': ('11:21'),
    'vav1_11': ('11:16'),
    'vav1_12': ('11:19'),
    'vav1_13': ('11:20'),
    'vav1_14': ('11:37'),
    'vav1_15': ('11:38'),
    'vav1_16': ('11:39'),
    'vav1_36': ('12:36'),
    'vav2_3': ('12:31'),
    'vav2_4': ('12:29'),
    'vav2_6': ('12:27'),
    'vav2_7': ('12:30'),
    'vav2_8': ('12:34'),
    'vav2_11': ('12:25'),
    'vav2_12': ('12:26'),
    'vav2_13': ('12:23'),
    'vav2_14': ('12:24'),
    }


trane_vavs = {
    'vav2_1': ('12:32'),
    'vav2_2': ('12:33'),
    'vav2_5': ('12:28'),
    #'vav2_9': ('12:35'),
    #'vav2_10': ('12:10'),
    }

If I use:如果我使用:

with open('config.txt') as f:
    myDevices = [tuple(map(str, i.split(','))) for i in f]

This will come through as:这将通过以下方式实现:

[('\n',),
 ('\n',),
 ('# format : key = name', ' v = (trunk', ' hardware address) tuple\n'),
 ('jci_vavs = {\n',),
 ("\t'vav1_1': ('11:14')", '\n'),
 ("\t'vav1_2': ('11:13')", '\n'),
 ("\t'vav1_3': ('11:15')", '\n'),
 ("\t'vav1_4': ('11:11')", '\n'),
 ("\t'vav1_5': ('11:9')", '\n'),
 ("\t'vav1_6': ('11:8')", '\n'),
 ("\t'vav1_7': ('11:7')", '\n'),
 ("\t'vav1_8': ('11:6')", '\n'),
 ("\t'vav1_9': ('11:9')", '\n'),
 ("\t'vav1_10': ('11:21')", '\n'),
 ("\t'vav1_11': ('11:16')", '\n'),
 ("\t'vav1_12': ('11:19')", '\n'),
 ("\t'vav1_13': ('11:20')", '\n'),
 ("\t'vav1_14': ('11:37')", '\n'),
 ("\t'vav1_15': ('11:38')", '\n'),
 ("\t'vav1_16': ('11:39')", '\n'),
 ("\t'vav1_36': ('12:36')", '\n'),
 ("\t'vav2_3': ('12:31')", '\n'),
 ("\t'vav2_4': ('12:29')", '\n'),
 ("\t'vav2_6': ('12:27')", '\n'),
 ("\t'vav2_7': ('12:30')", '\n'),
 ("\t'vav2_8': ('12:34')", '\n'),
 ("\t'vav2_11': ('12:25')", '\n'),
 ("\t'vav2_12': ('12:26')", '\n'),
 ("\t'vav2_13': ('12:23')", '\n'),
 ("\t'vav2_14': ('12:24')", '\n'),
 ('\t}\n',),
 ('\n',),
 ('\n',),
 ('trane_vavs = {\n',),
 ("\t'vav2_1': ('12:32')", '\n'),
 ("\t'vav2_2': ('12:33')", '\n'),
 ("\t'vav2_5': ('12:28')", '\n'),
 ("\t#'vav2_9': ('12:35')", '\n'),
 ("\t#'vav2_10': ('12:10')", '\n'),
 ('\t}\n',),
 ('\t\n',),
 ('\t\n',),
 ('\t\n',)]

What I am hoping to do with this config file is loop through config file print(devices) and print(device_name) when I can load the config file, something like:当我可以加载配置文件时,我希望对此配置文件执行的操作是循环通过配置文件print(devices)print(device_name) ,例如:

    for vav in jci_vavs.items():    
        device = vav[1]
        device_name = vav[0]



    for vav in trane_vavs.items():  
        device = vav[1]
        device_name = vav[0]

First, the easy and comfortable solution: Create a config.py file of the existing config.txt file.一、简单舒适的解决方案:将现有的config.txt文件创建一个config.py文件。 Then import config and convert the data to a map with the structure name: (trunk, hardware_adress) so it has the type <String, tuple(String, String)>然后import config并将数据转换为 map,其结构name: (trunk, hardware_adress) ,因此它的类型为<String, tuple(String, String)>

from shutil import copy, copyfile

# copy the existing text file to create a python file with the same data
copyfile('config.txt', 'config.py')

# import the created python file
import config

# load the first map out of the config-file
jci_vavs = config.jci_vavs
for vav in jci_vavs.items():
    name = vav[0]
    
    # split the value of the map by ':'
    v = vav[1]
    l = v.split(':')
    
    # convert the string to int
    trunk, hardware_adress = [int(s) for s in l]
    
    # create tuple -> add tuple to the map
    jci_vavs[name] = (trunk, hardware_adress)

trane_vavs = config.trane_vavs
for vav in trane_vavs.items():
    name = vav[0]
    
    # split the value of the map by ':'
    v = vav[1]
    l = v.split(':')
    
    # convert the string to int
    trunk, hardware_adress = [int(s) for s in l]
    
    # create tuple -> add tuple to the map
    trane_vavs[name] = (trunk, hardware_adress)


Another way to solve this problem, is to manually iterate over every line of the text file.解决此问题的另一种方法是手动迭代文本文件的每一行。 I personally do not recommend that way, Nonetheless: here is my implementation using that way:我个人不推荐这种方式,尽管如此:这是我使用这种方式的实现:

import re

filename = 'config.txt'

# 2 maps where the data will be stored 
# structure: key=name, items=(trunk, hardware_adress)
jci_vavs = {}
trane_vavs = {}

with open(filename) as f:
    # split the text file by lines
    data = f.read().splitlines()
    
def get_number(string) -> int:
    return int(re.findall(r'\d+', string)[0])

def get_string(string) -> str:
    # removing unwanted signs
    string = string.replace(' ', '')
    string = string.replace('"', '')
    string = string.replace("'", "")
    return string

def get_infos(string):
    name, trunk, hardware_adress = string.split(':')
    # convert to a string
    name = get_string(name)
 
    # convert to a string
    trunk = get_number(trunk)
    hardware_adress = get_number(hardware_adress)
    
    # return the information
    return name, trunk, hardware_adress


for i in range(len(data)):
    if "jci_vavs" in data[i]:
        i += 1

        while '}' not in data[i]:
            name, trunk, hardware_adress = get_infos(data[i])
   
            # add the information to the map using a tuple
            jci_vavs[name] = (trunk, hardware_adress)
    
            i += 1

    if "trane_vavs" in data[i]:
        i += 1

        while '}' not in data[i]:
            name, trunk, hardware_adress = get_infos(data[i])
   
            # add the information to the map using a tuple
            trane_vavs[name] = (trunk, hardware_adress)
            
            i += 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM