Question: PYTHON Make sure file classic_books.txt is located in the current directory. Since this file is a .txt file, use Python open() to read the file

PYTHON

Make sure file classic_books.txt is located in the current directory. Since this file is a .txt file, use Python open() to read the file line-by-line - The file has 4 columns of data: Rank, Title, Author, Year - Each data entry is separated by the '|' character, so use Python's split() method to parse the input line. - Save each line as an entry in a Python List. 1. Create an empty list named classics 2. Read file classic_books.txt using Python's open() function. - NOTE: Use with open(...) as XXXXX: to eliminate having to close() the file. - HINT: Remember to strip() whitespace

3. Parse each line of the file using the .split() method and save the values (list) in a variable named book_data 4. Add book_data to classics 5. Print the contents of classics - Use a FOR loop to iterate over the list so each entry is printed on a separate line

IMPORTANT: Use the classics list created in the previous cell to write a new CSV file named classic_books.csv.

6. Open file classic_books.csv and use Pythons csv.writer() function to write each entry in classics list as a row in the CSV file. - NOTE: Iterate thru the classics list and use .writerow to populate the file - HINT: Specify newline='' to keep from getting extra blank lines in CSV file

classic_books.txt

PYTHON Make sure file classic_books.txt is located in the current directory. Since

Cannot figure out what is wrong with this code that I have developed.

import csv classics = [] with open('classic_books.txt', 'r') as f: for line in f: line.split('|') line.strip() book_data = [line] print(book_data) classics = book_data print(classics) 

with open('classic_books.csv', 'w', newline='') as csvfile: writer = csv.writer(csvfile) writer.writerows(classics)

Part A.py classic_books.txt Rank|Title|Author|Year 1 Pride and Prejudice Jane Austen|1813 2.To Kill a Mockingbird| Harper Lee|1960 3|The Great Gatsby|F. Scott Fitzgerald|1925 4 One Hundred Years of Solitude Gabriel Garcia Marquez|1967 5|In Cold Blood|Truman Capote|1965 6 Wide Sargasso Sea Jean Rhys|1966 7| Brave New World| Aldous Huxley|1932 8 I Capture The Castle Dodie Smith|1948 9|Jane Eyre|Charlotte Bronte|1847 10|Crime and Punishment|Fyodor Dostoevsky|1866

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!