简体   繁体   English

将数据添加到 txt 文件中的字典 Python 3

[英]Adding data to dictionary in txt file Python 3

So i want to doing college project to build some sort of bank application, the input from the user should saved to the dictionary in txt file, but i only know how to do dict in python not the txt file How should i code this?所以我想做大学项目来构建某种银行应用程序,用户的输入应该保存到 txt 文件中的字典中,但我只知道如何在 python 中做 dict 而不是 txt 文件我应该如何编码?

my code is something like this我的代码是这样的



    rekening =  {
  
}
a=input("Masukkan Nama Anda :  ")
x = [i for i in range(1, 10)]
norek = (random.choice(x), random.choice(x), random.choice(x))
norekakhir = int(''.join(map(str, norek))) 
rekening[a] = "rek" + str(norekakhir) 
print("Rekening anda dengan nama", a, "Nomer Rekening", "rek" + str(norekakhir))
print("Telah terdaftar")
print("Terima Kasih")
print(rekening)

it only save to the dictionary not to txt file, sorry for my broken english and its my first time post here ^^它只保存到字典而不是 txt 文件,对不起我的英语不好,这是我第一次在这里发帖^^

You may use json.dumps()您可以使用json.dumps()

import json

my_dict = {
    "a": "x",
    "b": "y"
}

with open("my_dicitonary_file.txt", 'w') as file:
     file.write(json.dumps(my_dict))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM