从txt中保存数据,然后更新并再次保存

问题描述 投票:0回答:1

如何从.txt文件中加载数据,其中保存了14个体育项目(14个不同的字典)中的字典?我需要编辑并保存条目。

我目前这样保存程序:

def ulozit():
    vsetko = (medicimbal.items(),zhyb.items(),zhybyChalani.items(),clnkovyb.items(), stoM.items(), tisicM.items(),tritisicM.items(), hodG.items(),skokdoD.items()
              ,sedL.items(),veslp.items(),kluk.items(),preskok.items(),skokdoD.items())
    f = open("vsetko.txt", "w")
    f.write( str(vsetko) )
    f.close()

我需要阅读并更新以上内容,但是如何?

我的程序https://github.com/smajdalf89/Programovanie/edit/master/Tv%20Program在第464行以“ def ulozit()”开头。

在以如下方式保存的txt中,字段是名称,类别,性别和锻炼方式:

(dict_items([('michal',['1.a','CH',9.0])]),dict_items([('danka',['2.a',35.0])]),dict_items([('jozko',['3.a',30.0])])),dict_items([('marian',['4.a','ch',6.0])]),dict_items([('martin',['3.a','Ch',6.0]))),dict_items([('erik',['1.b','CH',3.05])])),dict_items([('trulo',['2.c','CH',12.08])]),dict_items([('richard',['1.a','Ch',30.0])])),dict_items([('michal',['4.a','CH',250.0])]),dict_items([('michal',['4.a','CH',250.0])]),dict_items([(jebo',['3.d','CH', 9.0])]),dict_items([('michal',['2.a','CH',1.2])]),dict_items([('milan',['3.c','CH',50.0])])),dict_items([('lukas',['1.c','CH',25.0]])))))

python tkinter load fwrite
1个回答
0
投票

我只是使用文本文件来保存数据atm。

对于字典,我将它们像CSV一样存储,在其中我将split()函数用于键和值

用于访问数据,我用

    with open(filename, "r") as txtr:
        data = txtr.read()

在此之后,我多次分割数据并将它们分配给字典。

要更新文件,您需要重写整个文本文件。因此有2个功能,即保存和加载。

© www.soinside.com 2019 - 2024. All rights reserved.