Working with csv and excel files

This quiz checks what you know about handling CSV and Excel files in Python. These skills help you read, write and manage spreadsheet data easily perfect for data analysis and automation!

Last Updated :
Discuss
Comments

Question 1

Which module is used in Python to read and write CSV files?

  • xlsxwriter

  • openpyxl

  • csv

  • pandas

Question 2

What is the output of the following code?

Python
import csv

with open('data.csv', 'r') as file:
    reader = csv.reader(file)
    headers = next(reader)
    print(headers)


  • Prints the first row (usually headers) from the CSV file

  • Throws an error

  • Prints all rows

  • Reads file as a string

Question 3

What class from the csv module reads each row into a dictionary?

  • csv.reader

  • csv.DictReader

  • csv.reader_dict

  • csv.ListReader

Question 4

What is the correct way to write a list of dictionaries to a CSV file?

  • csv.writerows(dict_list)

  • csv.DictWriter(file).writerows(dict_list)

  • csv.DictReader(file).write(dict_list)

  • file.write(dict_list)

Question 5

What is the role of the index=False parameter in to_csv() when using pandas?

  • It includes index values in the file

  • It prevents pandas from reading the file

  • It excludes row indices from being written

  • It sorts the DataFrame

Question 6

What will be the output of the following openpyxl code if the cell A1 has value 'Python'?

Python
import openpyxl
wb = openpyxl.load_workbook("demo.xlsx")
sheet = wb.active
print(sheet.cell(row=1, column=1).value)


  • Prints the cell’s row number

  • Prints 'Python'

  • Returns None

  • Throws error

Question 7

Which method in openpyxl is used to determine the number of rows in an Excel sheet?

  • len(sheet)

  • sheet.count()

  • sheet.max_row

  • sheet.get_rows()

Question 8

What will this code output if there are 4 columns in the Excel sheet?

Python
print(sheet_obj.max_column)


  • 3

  • 4

  • 5

  • Number of rows

Question 9

How do you loop through the first column values in openpyxl?

  • Using range(1, sheet.max_column + 1)

  • Using sheet.columns[0]

  • Using range(1, sheet.max_row + 1) and setting column=1

  • Looping through sheet.rows directly

Question 10

Which openpyxl method reads a specific row of values from the Excel sheet?

  • sheet.cell(row=i, column=j) in a column loop

  • sheet.read_row()

  • sheet.row_values()

  • sheet.get_row()

There are 10 questions to complete.

Take a part in the ongoing discussion