INTRODUCTION
“This Python script allows you to clean up a CSV file by removing all empty rows. You’ll provide the name of the input CSV file, and after processing, the script will save the cleaned data into a new CSV file. Additionally, you can choose to open the output file immediately after it is saved. This simple process helps ensure that your data remains tidy and ready for further analysis.”
PROGRAM
HERE IS THE FULL PROGRAM TO REMOVE EMPTY ROWS FROM CSV FILE
import pandas as pd import os input_file = input("Enter the name of the input CSV file (with .csv extension): ") output_file = input("Enter the name for the output CSV file (with .csv extension): ") df = pd.read_csv(input_file) df_cleaned = df.dropna(how='all') df_cleaned.to_csv(output_file, index=False) print(f"Empty rows removed. File saved as {output_file}.") open_file = input("Do you want to open the output file? (yes/no): ").lower() if open_file == 'yes': os.startfile(output_file) else: print("You chose not to open the file.")
EXPLANATION OF THE PROGRAM
1. Input File Name
The program starts by asking the user to enter the name of the input CSV file (including the .csv
extension). The input()
function takes the user’s input as a string and stores it in the variable input_file
.
input_file = input("Enter the name of the input CSV file (with .csv extension): ")