cfg文件的解析

 

cfgfile = './yolov3.cfg'
file = open(cfgfile, 'r')
lines = file.read().split('\n')  # store the lines in a list等价于readlines
lines = [x for x in lines if len(x) > 0]     # 去掉空行
lines = [x for x in lines if x[0] != '#']    # 去掉以#开头的注释行
lines = [x.rstrip().lstrip() for x in lines]  

# 去掉左右两边的空格(rstricp是去掉右边的空格,lstrip是去掉左边的空格)

 完整代码

def parse_cfg(cfgfile):
    """
    输入: 配置文件路径
    返回值: 列表对象,其中每一个元素为一个字典类型对应于一个要建立的神经网络模块(层)
    
    """
    # 加载文件并过滤掉文本中多余内容
    file = open(cfgfile, 'r')
    lines = file.read().split('\n')                       
    lines = [x for x in lines if len(x) > 0]               
    lines = [x for x in lines if x[0] != '#']              
    lines = [x.rstrip().lstrip() for x in lines]           
    
    block = {}
    blocks = []
    
    for line in lines:
        if line[0] == "[":           # 这是cfg文件中一个层(块)的开始           
            if len(block) != 0:      
                blocks.append(block)   
                block = {}           
            block["type"] = line[1:-1].rstrip()  # 把cfg的[]中的块名作为键type的值   
        else:
            key,value = line.split("=") 
            block[key.rstrip()] = value.lstrip()

    blocks.append(block) # 退出循环,将最后一个未加入的block加进去
    # print('\n\n'.join([repr(x) for x in blocks]))
    return blocks

blocks部分输出

[
{'scales': '.1,.1', 'exposure': '1.5', 'steps': '400000,450000', 'angle': '0', 'channels': '3'},
{'filters': '32', 'activation': 'leaky', 'stride': '1', 'pad': '1', 'batch_normalize': '1', 'size': '3', 'type': 'convolutional'},
{'filters': '64', 'activation': 'leaky', 'stride': '2', 'pad': '1', 'batch_normalize': '1', 'size': '3', 'type': 'convolutional'},
{'filters': '32', 'activation': 'leaky', 'stride': '1', 'pad': '1', 'batch_normalize': '1', 'size': '1', 'type': 'convolutional'},
{'filters': '64', 'activation': 'leaky', 'stride': '1', 'pad': '1', 'batch_normalize': '1', 'size': '3', 'type': 'convolutional'}
]

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
假设`Edvr.cfg`文件内容如下: ``` [General] Version=1.0 Name=Edvr [Parameters] InputFile=input.mp4 OutputFile=output.mp4 FrameRate=30 ``` 可以通过以下步骤将`Edvr.cfg`文件解析为一个`Config`结构体: 1. 定义`Config`结构体,包含`General`和`Parameters`两个结构体。`General`结构体包含版本号和名称,`Parameters`结构体包含输入文件名、输出文件名和帧率。 ```c typedef struct { struct { float version; char name[50]; } general; struct { char input_file[100]; char output_file[100]; int frame_rate; } parameters; } Config; ``` 2. 编写解析函数,读取`Edvr.cfg`文件,并将读取的内容存储到`Config`结构体中。 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> Config parse_config_file(const char* file_name) { Config config = {0}; char line[100]; char section[20] = ""; FILE* fp; fp = fopen(file_name, "r"); if (fp == NULL) { printf("Failed to open file: %s\n", file_name); return config; } while (fgets(line, sizeof(line), fp) != NULL) { // 去除行末换行符 if (line[strlen(line) - 1] == '\n') { line[strlen(line) - 1] = '\0'; } // 处理段落信息 if (line[0] == '[' && line[strlen(line) - 1] == ']') { strcpy(section, line + 1); section[strlen(section) - 1] = '\0'; } else { // 处理键值对信息 char* key = strtok(line, "="); char* value = strtok(NULL, "="); if (strcmp(section, "General") == 0) { if (strcmp(key, "Version") == 0) { config.general.version = atof(value); } else if (strcmp(key, "Name") == 0) { strncpy(config.general.name, value, sizeof(config.general.name) - 1); config.general.name[sizeof(config.general.name) - 1] = '\0'; } } else if (strcmp(section, "Parameters") == 0) { if (strcmp(key, "InputFile") == 0) { strncpy(config.parameters.input_file, value, sizeof(config.parameters.input_file) - 1); config.parameters.input_file[sizeof(config.parameters.input_file) - 1] = '\0'; } else if (strcmp(key, "OutputFile") == 0) { strncpy(config.parameters.output_file, value, sizeof(config.parameters.output_file) - 1); config.parameters.output_file[sizeof(config.parameters.output_file) - 1] = '\0'; } else if (strcmp(key, "FrameRate") == 0) { config.parameters.frame_rate = atoi(value); } } } } fclose(fp); return config; } ``` 上面的代码中,使用`fgets()`函数逐行读取`Edvr.cfg`文件的内容,然后根据行内容的不同,分别处理段落信息和键值对信息。具体来说: - 如果行内容以方括号开头和结尾,说明这是一个段落信息,需要将方括号中的内容作为当前段落的名称。 - 如果行内容中包含等号,说明这是一个键值对信息,需要将等号左边的字符串作为键,等号右边的字符串作为值。根据当前段落的名称和键的名称,将值存储到`Config`结构体的相应字段中。 3. 调用解析函数,读取`Edvr.cfg`文件并打印配置信息。 ```c int main() { Config config; config = parse_config_file("Edvr.cfg"); printf("General:\n"); printf("Version: %.1f\n", config.general.version); printf("Name: %s\n", config.general.name); printf("Parameters:\n"); printf("InputFile: %s\n", config.parameters.input_file); printf("OutputFile: %s\n", config.parameters.output_file); printf("FrameRate: %d\n", config.parameters.frame_rate); return 0; } ``` 上面的代码中,调用`parse_config_file()`函数解析`Edvr.cfg`文件,并将解析结果存储到`config`结构体中。然后,打印`config`结构体中的各个字段。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值