python保存字典和讀取字典的實例代碼
更新時間:2019年07月07日 09:26:51 作者:KK_f2d5
這篇文章主要介紹了python保存字典和讀取字典的實例代碼,通過代碼給大家介紹了python 使用列表和字典存儲信息的相關代碼,需要的朋友可以參考下
讀取一個已經保存了的字典
f = open('dict_th','r')
a = f.read()
dict_hi = eval(a)
f.close()
保存一個字典
dict = {}
list1 = []
list2 = []
for line in lines:
line = line.strip()
if ">" in line:
list1.append(line)
else:
list2.append(line)
for i in range():
dict[list1[i]] = list2[i]
f6 = open("dict_th",'w')
f6.write(str(dict))
f6.close()
ps:下面看下python 使用列表和字典存儲信息
"""
作者:白
時間:2018年1月9日
需求:假設你很多汽車,通過不斷詢問您是否要將車輛添加到您的庫存中,
如果您這樣做,那么它將會詢問汽車的細節(jié)。如果沒有,應用程序將打印所有汽車的詳細信息并退出。
功能:循環(huán)添加汽車相關信息,并記錄信息到字典中
"""
def main():
car_list = []
while True:
add_inventory = input('是否添加汽車信息?(y/n):')
if add_inventory == 'y':
car_model = input('請輸入汽車的型號:')
car_color = input('請輸入汽車的顏色:')
car_year = input('請輸入汽車的年限:')
car_miles = input('請輸入汽車的公里:')
car_dict={'model':car_model,'color':car_color,'year':car_year,'miles':car_miles}
print(car_dict)
car_list.append(car_dict)
elif add_inventory == 'n':
print(car_list)
break
if __name__ == '__main__':
main()
總結
以上所述是小編給大家介紹的python保存字典和讀取字典的實例代碼,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
相關文章
opencv3/Python 稠密光流calcOpticalFlowFarneback詳解
今天小編就為大家分享一篇opencv3/Python 稠密光流calcOpticalFlowFarneback詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Python Tkinter創(chuàng)建GUI應用程序的示例
Tkinter提供了豐富的功能和靈活的接口,讓開發(fā)者能夠輕松地構建出各種各樣的圖形用戶界面,本文介紹了使用Python的Tkinter庫創(chuàng)建圖形用戶界面GUI應用程序,感興趣的可以了解一下2024-12-12
pytest使用parametrize將參數(shù)化變量傳遞到fixture
這篇文章主要為大家介紹了pytest使用parametrize將參數(shù)化變量傳遞到fixture的使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05
python中sort sorted reverse reversed函數(shù)的區(qū)別說明
這篇文章主要介紹了python中sort sorted reverse reversed函數(shù)的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05

