0

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?

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 ^^

  • Do you really need a flat file? If so, you just output your dict to file in the structure you need. if you need to save your dict for reuse, then you can check this anwser: [Saving an Object (Data persistence)](https://stackoverflow.com/questions/4529815/saving-an-object-data-persistence) – astentx Dec 28 '20 at 08:43
  • yeah i just need a flat file, just a name and the number of bank account. Is it the pickle library? – hakaizero Dec 28 '20 at 08:54

1 Answers1

0

You may use 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))
sagmansercan
  • 101
  • 4