如何將字典保存到文件? - How to save a dictionary to a file?

問題:

I have problem with changing a dict value and saving the dict to a text file (the format must be same), I only want to change the member_phone field.我在更改 dict 值並將 dict 保存到文本文件(格式必須相同)時member_phone ,我只想更改member_phone字段。

My text file is the following format:我的文本文件格式如下:

memberID:member_name:member_email:member_phone

and I split the text file with:我將文本文件拆分爲:

mdict={}
for line in file:
    x=line.split(':')
    a=x[0]
    b=x[1]
    c=x[2]
    d=x[3]
    e=b+':'+c+':'+d

    mdict[a]=e

When I try change the member_phone stored in d , the value has changed not flow by the key,當我嘗試更改存儲在dmember_phone ,該值已更改而不是按鍵流動,

def change(mdict,b,c,d,e):
    a=input('ID')
    if a in mdict:
        d= str(input('phone'))
        mdict[a]=b+':'+c+':'+d
    else:
        print('not')

and how to save the dict to a text file with same format?以及如何將dict保存到具有相同格式的文本文件中?


解決方案:

參考一: https://en.stackoom.com/question/1IZ8E
參考二: https://stackoom.com/question/1IZ8E
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章