python静态方法@staticmethod,python读取prototxt文件,python字符串分割splite(),python在字符串查找字符串find()

1,运行效果

wangxy@avs05:~/test_code/test$ ls
config.prototxt  utils.py 
wangxy@avs05:~/test_code/test$ python utils.py 
object_id: 7
source_name: a.mp4
get prototxt error: source no type
source_type: None
wangxy@avs05:~/test_code/test$ 

2,config.prototxt

global{
  object_id: 7
  gpu_id: "6,7"
}
source{
  name: "a.mp4"
}

2,utils.py

# !/usr/bin/env python
# coding: utf-8

class Utils(object):
    @staticmethod
    def get_prototxt_string(src_str, section, opention):
        result = None
        src_lines = src_str.split('\n')
        index = 0
        while len(src_lines) > 0:
            line = src_lines[index]
            if line.find(section) >= 0:
                src_lines = src_lines[index + 1:]
                for line in src_lines:
                    if '}' is line:
                        print("get prototxt error: {} no {}".format(section, opention))
                        return str(result)
                    if line.find(opention) >= 0:
                        i = line.find("\"")
                        result = line[i + 1:-1]
                        return str(result)
            else:
                index += 1
        return str(result)

    @staticmethod
    def get_prototxt_int(src_str, section, opention):
        result = None
        src_lines = src_str.split('\n')
        index = 0
        while len(src_lines) > 0:
            line = src_lines[index]
            if line.find(section) >= 0:
                src_lines = src_lines[index + 1:]
                for line in src_lines:
                    if '}' is line:
                        print("get prototxt error: {} no {}".format(section, opention))
                        return int(result)
                    if line.find(opention) >= 0:
                        i = line.find(":")
                        result = line[i + 1:]
                        return int(result)
            else:
                index += 1
        return int(result)


if __name__ == "__main__":
    with open("config.prototxt", 'r') as file:
        info = file.read()
        object_id = Utils.get_prototxt_int(info, "global", "object_id")
        print("object_id: %d"%(object_id))
        source_name = Utils.get_prototxt_string(info, "source", "name")
        print("source_name: {}".format(source_name))
        source_type = Utils.get_prototxt_string(info, "source", "type")
        print("source_type: {}".format(source_type))
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值