-1

I created a CSV file that stores a book, its author and the year the book released, through user input. I want to now ask the user to choose an author and the program should display all the books by that author, but I'm not sure how to do this

Here is my code so far:

import csv

amount = int(input("How many records would you like to store: "))
count = 0

with open("Books.csv", "w", newline="") as file:
    writer = csv.writer(file)
    writer.writerow(["", "Book", "Author", "Year released"])

while count < amount:
    count += 1

    book = input("Enter a book: ")
    author = input("Enter it's Author: ")
    year = input("Enter the year it released: ")

    headers = [count, book, author, year]
    with open("Books.csv", 'a') as f:
        writer = csv.writer(f)
        writer.writerow(headers)
sdrr453e
  • 41
  • 6

1 Answers1

0

This should do.

Example contents of Books.csv

,Book,Author,Year released
0,1984,G. Orwell,1949
1,Brave New World,A. Huxley,1932
2,Johnny Got His Gun,D. Trumbo,1939

The code

import csv

# getting author and book column indexes in the csv
COLUMNS = ["", "Book", "Author", "Year released"]    # first column is the book no.
AUTHOR_COLUMN_INDEX = COLUMNS.index("Author")
BOOK_COLUMN_INDEX = COLUMNS.index("Book")

author = input("Enter the name of an author: ")

with open('Books.csv', 'r', newline='') as f:
    for line in csv.reader(f):
        if len(line) == len(COLUMNS):   # if empty line or invalid line
            if line[AUTHOR_COLUMN_INDEX] == author:
                print(line[BOOK_COLUMN_INDEX])

When the code is run:

Enter the name of an author: A. Huxley
Brave New World
rada-dev
  • 122
  • 5
  • I tried this but I'm getting an index error, saying that the list is out of the index range – sdrr453e Dec 19 '20 at 15:50
  • How do the rows of the csv look like? – rada-dev Dec 19 '20 at 16:03
  • row1 would have the headers book, Author, Year Released and the rows after that would have the data under each header – sdrr453e Dec 19 '20 at 16:18
  • so is it like this? COLUMNS = ["Book", "Author", "Year released"] – rada-dev Dec 19 '20 at 17:00
  • Yes, so under [book] it would have the book name, under [Author] it would have the author name and under [year released] it would have the date the book came out – sdrr453e Dec 19 '20 at 17:27
  • the line the error occurs is on if line [AUTHOR_COLUMN_INDEX] == author: – sdrr453e Dec 19 '20 at 18:06
  • it should be AUTHOR_COLUMN_INDEX = 1 (from what i was able to deduce, your first column is the book name - index 0, second column is the author name - index 1 – rada-dev Dec 19 '20 at 18:16
  • Well, I'm not sure where I went wrong, could you check my code again to see the problem? – sdrr453e Dec 19 '20 at 18:37
  • the problem is caused by the empty lines in your csv – rada-dev Dec 19 '20 at 18:51
  • That's just how it appears after the user inputs the data to be saved in the csv file, how can I fix that? – sdrr453e Dec 19 '20 at 18:53
  • @sdrr453e Use `newline=''` in your `open` statements with `csv` module. See https://stackoverflow.com/a/3348664/235698 for why... Your post only did it for the headers and that's why the data rows have blank lines. – Mark Tolonen Dec 19 '20 at 23:53