[root@tieba delete]# vim /tmp/content.txt //添加一些内容


{

    '北京':{

           '海淀区':{

                     '三环':{},

                     '四环':{}

           },

           '朝阳区':{

                     '三环':{},

                     '四环':{}

           }

    }

}


脚本内容

/
* 
-
-
-
示例代码
-
-
-
-
*
/
 
#!/usr/bin/env python
# -*- coding:utf-8 -*-
 
CONTENT 
= 
'/tmp/content.txt'
 
with 
open
(CONTENT) as fd:
    
menu 
= 
eval
(fd.read().strip())
 
current_layer 
= 
menu
parent_layers 
= 
[]
exit_flag 
= 
False
 
while 
not 
exit_flag:
    
for 
key 
in 
current_layer:
        
print 
key
    
choice 
= 
raw_input
(
">>>"
)
    
if 
choice 
in 
current_layer:
        
parent_layers.append(current_layer)
        
current_layer 
= 
current_layer[choice]
    
elif 
choice 
=
= 
'b'
:
        
if 
parent_layers:
            
current_layer 
= 
parent_layers.pop()
        
else
:
            
print 
"最上层了"
    
elif 
choice 
=
= 
'a'
:
        
add 
= 
raw_input
(
"增加>>>"
)
        
current_layer[add] 
= 
''
        
print 
"add %s Succeed" 
% 
add
    
elif 
choice 
=
= 
'c'
:
        
change 
= 
raw_input
(
"修改>>>"
)
        
add    
= 
raw_input
(
"填写>>>"
)
        
current_layer.pop(change)
        
current_layer[add] 
= 
''
        
print 
"%s修改为%s" 
% 
(change,add)
    
elif 
choice 
=
= 
'd'
:
        
delete 
= 
raw_input
(
"删除>>>"
)
        
if 
delete 
in 
current_layer:
            
current_layer.pop(delete)
            
print 
"delete %s Succeed" 
% 
delete
        
else
:
            
print 
"Not found %s" 
% 
delete
    
elif 
choice 
=
= 
'q'
:
        
with 
open
(CONTENT,
'w'
) as fd:
            
fd.write(
str
(menu))
        
exit_flag 
= 
True
    
else
:
        
print 
"无此项"
 
/
* 
-
-
-
示例代码
-
-
-
-
*
/


可以添加城市,修改城市,删除等功能