学習python(4)練習辞書機能クエリー追加削除更新
5166 ワード
#-*-coding:utf-8 -*-
"""
dtc
:
P0009 Engine Position System Performance Bank 2
P000A A Camshaft Position Slow Response Bank 1
P000B B Camshaft Position Slow Response Bank 1
P000C A Camshaft Position Slow Response Bank 2
P000D B Camshaft Position Slow Response Bank 2
"""
import os
def search():
w = raw_input("search word : ")
dic = {}
f = open("/Python_Data/work/123_OBDII_dtc_en_2.txt",'r')
lines = f.readlines()
search_success = 0
for line in lines:
key = line[:5]
comment = line[6:]
dic[key] = comment
if w in dic.keys():
search_success = 1
print "word\tmean
"
print w,dic[w],
print "======== =============="
f.close()
break
if search_success ==0:
print "Not found~!"
def add():
while 1:
w = raw_input("add word : ")
dic = {}
f = open("/Python_Data/work/123_OBDII_dtc_en_2.txt",'r')
lines = f.readlines()
search_success = 0
for line in lines:
key = line[:5]
comment = line[6:]
dic[key] = comment
if w in dic.keys():
search_success = 1
f.close()
break
if search_success ==1:
print" , ~"
else:
f.close()
break
mean = raw_input("this word mean : ")
f1 = open("/Python_Data/work/123_OBDII_dtc_en_2.txt",'a')
f1.write(w+' '+ mean + '
')
print"========= ============="
f1.close()
def delete():
w = int(raw_input("delete : "))
i =1
f = open("/Python_Data/work/123_OBDII_dtc_en_2.txt",'r')
temp = open("/Python_Data/work/temp.txt",'w')
while 1:
line = f.readline()
if line:
if i == w:
i = i +1
print " %d: %s"%(w,line)
continue
else:
i = i +1
writeline = '%s'% line
temp.write(writeline)
else:
print " "
break
f.close()
temp.close()
os.remove("/Python_Data/work/123_OBDII_dtc_en_2.txt")
os.rename("/Python_Data/work/temp.txt","/Python_Data/work/123_OBDII_dtc_en_2.txt")
print "============ ===============!"
def update():
w = int(raw_input("update : "))
f = open("/Python_Data/work/123_OBDII_dtc_en_2.txt",'r+')
lines = f.readlines()
mean = raw_input("new mean:")
lines[w] = mean + "
"
f = open("/Python_Data/work/123_OBDII_dtc_en_2.txt",'w+')
f.writelines(lines)
print "======== =============="
f.close()
def main():
while 1:
print "||=================||
"
print "|| ...||
"
print "|| a ||
"
print "|| b ||
"
print "|| c ||
"
print "|| d ||
"
print "|| e ||
"
print "||=================||
"
i = raw_input(" : ")
if i == 'a'or i == 'A':
search()
elif i == 'b' or i =='B':
add()
elif i == 'c' or i =='C':
delete()
elif i == 'd' or i == 'D':
update()
elif i =='e' or i =='E':
exit()
if __name__== '__main__':
main()
以下はオープンソースのネット上のバージョンを学びます:差を比較してみます
# -*- coding: cp936 -*-
import os
#
fname = 'myphonebookdata'
if fname in os.listdir('.'):
data = {line.strip().split('~')[0]:line.strip().split('~')[1] for line in open(fname,'r').readlines()}
else:
data = {}
#
ask = raw_input(' :-) (A)/ (S)/ (D)/ ()? ').strip().lower()
while len(ask)>0:
if ask=='a':
print ' ...'
name = raw_input(' : ').strip()
while name in data:
print ' , ...'
name = raw_input(' : ').strip()
number=raw_input(' : ').strip()
while len(number)==0:
print ' , ...'
number=raw_input(' : ').strip()
data[name] = number
print ' !'
elif ask=='s':
print ' ...'
name = raw_input(' : ').strip()
if name in data:
print ' \t\t '
print '%s\t\t%s' % (name,data[name])
print ' !'
else:
print ' !'
elif ask=='d':
print ' ...'
name = raw_input(' : ').strip()
if name in data:
del data[name]
print ' !'
else:
print ' !'
else:
print ' , '
print
ask = raw_input(' :-) (A)/ (S)/ (D)/ ()? ').strip().lower()
#
s = ''
for k in data:
s += '%s~%s
' % (k,data[k])
fout = open(fname,'w')
fout.write(s)
fout.close()
簡潔で、最後にファイルの書き込み操作を行います.