1

I have a text file which reads as follows;

#Listing showing sample book details 
#AUTHOR, TITLE, FORMAT, PUBLISHER, COST?, STOCK, GENRE
P.G. Wodehouse, Right Ho Jeeves, hb, Penguin, 10.99, 5, fiction
A. Pais, Subtle is the Lord, pb, OUP, 12.99, 2, biography
A. Calaprice, The Quotable Einstein, pb, PUP, 7.99, 6, science
M. Faraday, The Chemical History of a Candle, pb, Cherokee, 5.99, 1, science
C. Smith, Energy and Empire, hb, CUP, 60, 1, science
J. Herschel, Popular Lectures, hb, CUP, 25, 1, science
C.S. Lewis, The Screwtape Letters, pb, Fount, 6.99, 16, religion
J.R.R. Tolkein, The Hobbit, pb, Harper Collins, 7.99, 12, fiction
C.S. Lewis, The Four Loves, pb, Fount, 6.99, 7, religion
E. Heisenberg, Inner Exile, hb, Birkhauser, 24.95, 1, biography
G.G. Stokes, Natural Theology, hb, Black, 30, 1, religion

Im currently running the following code to calculate the total value of stock by multiplying the cost of the book by the amount of stock we have.

book_list = []
def fileread():
    book_count = 0
    book_data = open("bookData", 'r')

    for row in book_data:
        if not row.startswith('#'):
            row = row.rstrip('\n').split(',')
            book_list.append(row)
            book_count += 1

    print('Author, Title, Format, Publisher, Cost, Stock, Genre \n')
    for each_book in book_list:
        print(each_book)

    total_value = 0
    total_books = 0
    # for each book(row) in the book list, multiple the value in position 4 of the array by the value in position 5
    for each_book in book_list:
        total_value += float(each_book(4)) * float(each_book(5)))
        total_books += 1

    print("The total number of book titles is ", book_count)
    print("The total value of all books in stock is ", "£", format(total_value, ',.2f'))

fileread()

The code prints each row in the text file into its own list and seperates the values by commas but gives me this error.

  File "/home/conor/Desktop/COM101/assignment.py", line 26, in <module>
    fileread()
  File "/home/conor/Desktop/COM101/assignment.py", line 20, in fileread
    total_value += callable(float(each_book(4)) * float(each_book(5)))
TypeError: 'list' object is not callable

Could use some help thanks.

Conor
  • 55
  • 6
  • 1
    You need to write the code that reads a line and splits it into fields and then use this code for every line in the file. Or you could try to use code that is already written, for example in the [csv](https://docs.python.org/3/library/csv.html) module, because this file looks like CSV. – mkrieger1 Dec 01 '20 at 14:38

0 Answers0