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