如何保存用户输入值?

时间:2019-12-31 05:20:28

标签: python python-3.8

我必须为我的高中项目建立一个书店数据库的模型。 可以输入一本新书的信息,但是我希望它在终止程序后仍然保留。目前,我只是在代码本身中输入了几本书,以便在运行时不会为空。

我已经完成了很多工作,但我只想知道如何保存用户输入数据以备后用。

books={1001:['Inferno','Dan Brown','Anchor Books','Thriller',42.00,70],
       1002:['As You Like It','William Shakespear','Penguin Publications','Classics',20.00,54],
       1003:['The Kite Runner','Khaled Hosseini','Bloomsbury Publcations','Drama',30.00,70],
       1004:['A Thousand Splendid Suns','Khaled Hosseini','Bloomsbury Publications','Fiction',35.00,70],
       1005:['The Girl on The Train','Paula Hawkins','Riverhead Books','Thriller',28.00,100],
       1006:['The Alchemist','Paulo Coelho','Rupa Books','Drama',25.00,50]}
while True:
    print ('ID    |   Name')
    [print(key,' | ',books[key][0]) for key in books.keys()]
    print (

'''

MAIN MENU:-
1. Add new book details
2. Modify data of a specific book
3. Delete all data of a book
4. View all data of a book
***********************************
5. Check Availability
6. Sales
7. Exit

''')

    cho=int(input('Enter your choice(1-7):'))
    if cho==1:
        book_id=int(input('Enter the book id:'))
        b_name=input('Enter the book name:-')
        a_name=input('Enter the authors name:')
        p_name=input('Enter name of publication:')
        genre=input('Enter the genre of the book:')
        price=float(input('Enter the price of the book (with decimal place):'))
        no_copy=int(input('Enter no of copies available:'))
        books[book_id]=[b_name,a_name,p_name,genre,price,no_copy]
    elif cho==2:

        book_id=int(input('Enter book id:'))
        print (books[book_id])
        print('''0. Book name
1. Author's name
2. Publisher
3. Genre
4. Price
5. No of copies''')
        index=int(input('Enter index of data to be changed:'))
        if index>=0 and index<=3:
            new_val=input('Enter the modified value:')
        elif index==4:
            new_val=float(input('Enter the modified value (with decimal place):'))
        elif index==5:
            new_val=int(input('Enter the modified value:'))
        books[book_id][index]=new_val
    elif cho==3:
         book_id=int(input('Enter book id of book to be deleted:'))
         print ('Book', book_id, 'data deleted')
         del books[book_id]
    elif cho==4:
        book_id=int(input('Enter the book id:'))
        print(books[book_id])
    elif cho==5:
        book_id=int(input('Enter book id of book to check availability:'))
        print ('''
''', books[book_id][5], 'copies are currently in stock')
    elif cho==6:
        book_id=int(input('Enter book id of book to buy:'))
        nos=int(input('Number of copies to buy'))
        books[book_id][5]=books[book_id][5]-nos
        print (nos,'copies of books purchased')
    elif cho==7:
        break

对不起,如果代码令人讨厌,我对Python来说是新手。另外,如果您有任何建议可以做得更好,那就太棒了。

4 个答案:

答案 0 :(得分:1)

您可以将输入写入文件(对于您的情况,最好是csv文件)。 CSV File I/O Link

答案 1 :(得分:1)

您可以将变量保存/加载到文件中。

一个选项是“ hickle

要安装:

pip install hickle

使用:

import hickle as hkl

# write variables to filename [a,b,c can be of any size]
hkl.dump([a,b,c], filename)

# load variables from filename
a,b,c = hkl.load(filename)

修改了您的代码:

import hickle as hkl

filename='books.file'
books ={}

try:
    books = hkl.load(filename)
except IOError:
    print("File not exists")


while True:
    print ('ID    |   Name')
    [print(key,' | ',books[key][0]) for key in books.keys()]
    print (

'''

MAIN MENU:-
1. Add new book details
2. Modify data of a specific book
3. Delete all data of a book
4. View all data of a book
***********************************
5. Check Availability
6. Sales
7. Exit

''')

    cho=int(input('Enter your choice(1-7):'))
    if cho==1:
        book_id=int(input('Enter the book id:'))
        b_name=input('Enter the book name:-')
        a_name=input('Enter the authors name:')
        p_name=input('Enter name of publication:')
        genre=input('Enter the genre of the book:')
        price=float(input('Enter the price of the book (with decimal place):'))
        no_copy=int(input('Enter no of copies available:'))
        books[book_id]=[b_name,a_name,p_name,genre,price,no_copy]
        hkl.dump(books, filename)
    elif cho==2:

        book_id=int(input('Enter book id:'))
        print (books[book_id])
        print('''0. Book name
1. Author's name
2. Publisher
3. Genre
4. Price
5. No of copies''')
        index=int(input('Enter index of data to be changed:'))
        if index>=0 and index<=3:
            new_val=input('Enter the modified value:')
        elif index==4:
            new_val=float(input('Enter the modified value (with decimal place):'))
        elif index==5:
            new_val=int(input('Enter the modified value:'))
        books[book_id][index]=new_val
    elif cho==3:
         book_id=int(input('Enter book id of book to be deleted:'))
         print ('Book', book_id, 'data deleted')
         del books[book_id]
    elif cho==4:
        book_id=int(input('Enter the book id:'))
        print(books[book_id])
    elif cho==5:
        book_id=int(input('Enter book id of book to check availability:'))
        print ('''
''', books[book_id][5], 'copies are currently in stock')
    elif cho==6:
        book_id=int(input('Enter book id of book to buy:'))
        nos=int(input('Number of copies to buy'))
        books[book_id][5]=books[book_id][5]-nos
        print (nos,'copies of books purchased')
    elif cho==7:
        break

答案 2 :(得分:1)

有两种方法可以解决此问题:

  1. 将程序连接到数据库。我想说,在这种情况下最合适的是SQLite
  2. 将数据本地保存到文件中。我建议使用pandas将数据转换为数据帧并进行操作。之后,您可以将数据框保存到CSV。这是大熊猫的docs

就改进您的代码而言,我建议将其分解为functions。它将更易于调试和理解。祝您项目顺利!

答案 3 :(得分:0)

使用csv文件存储所有数据。

import csv   
if cho==1:
        book_id=int(input('Enter the book id:'))
        b_name=input('Enter the book name:-')
        a_name=input('Enter the authors name:')
        p_name=input('Enter name of publication:')
        genre=input('Enter the genre of the book:')
        price=float(input('Enter the price of the book (with decimal place):'))
        no_copy=int(input('Enter no of copies available:'))
        books[book_id]=[b_name,a_name,p_name,genre,price,no_copy]
for book in books:
    with open('books.csv', 'a') as bks:
        writer = csv.writer(bks)
        writer.writerow(book)