Home > other >  python database make entries in new line
python database make entries in new line

Time:01-27

I have been dealing with a little problem while programming python database. If I make new entry it will stay in the line with previous and I dont know where should I add \n or how could I make it in second line. Thanks for help. These are lines we are dealing with. #edit: using import json I am new to asking on stackoverflow, I dont think there are more data needed but if I made mistake I will place rest of code

def write_to_file(data):
    with open('database.txt', 'w') as outfile:
        json.dump(data, outfile)

def read_from_file():
    with open('database.txt', 'r') as infile:
        return json.load(infile)

def add_entry(first, second, third, fourth, fifth, sixth, seventh, eighth, ninth):
    data = read_from_file()
    data.append({'id': first , 'titul': second, 'meno': third, 'priezvisko': fourth, 'ulica a cislo': fifth, 'mesto': sixth, 'psc': seventh, 'tlc': eighth, 'email': ninth})
    write_to_file(data)

I tried adding \n to nearly every part of the code but nothing helped

CodePudding user response:

Whitespace (i.e., spaces, lines, tabs etc) doesn't matter in the JSON format. So, for compactness, the JSON module dumps to the file as one condensed chunk of text. This is a feature for many applications (like web services) where transmitting/storing a lower amount of data is valuable.

You could specify something like indent=4 to better format the text.

def write_to_file(data):
    with open('database.txt', 'w') as outfile:
        json.dump(data, outfile, indent=4)
  • Related