Python script to check csv columns for empty cells that will be used with multiple excels

Viewed 54

I've done research and can't find anything that has solved my issue. I need a python script to read csv files using a folder path. This script needs to check for empty cells within a column and then display a popup statement notifying users of the empty cells. Anything helps!!

3 Answers

Use the pandas library pip install pandas

You can import the excel file as a DataFrame and check each cell with loops.

Hello I think it's quite easy to solve with pandas:

import pandas as pd 
df = pd.read_csv('<path>')
df.describe() # to  just see empoty stuff 
np.where(pd.isnull(df)) # to show indexes of empty celss -> from https://stackoverflow.com/questions/27159189/find-empty-or-nan-entry-in-pandas-dataframe

alternatively you can read the file and check line by line for empty cells

A simple example using Python csv module:

cat test.csv                                                                                                                                                              
1,,test
2,dog,cat
3,foo,

import csv

with open('test.csv') as csv_file:
    empty_list = []
    c_reader = csv.DictReader(csv_file, fieldnames=["id", "fld_1", "fld_2"])
    for row in c_reader:
        row_dict = {row["id"]: item for item in row if not row[item]}
        if row_dict:
            empty_list.append(row_dict)

empty_list                                                                                                                                                                
[{'1': 'fld_1'}, {'3': 'fld_2'}]

This example assumes that there is at least one column that will always have a value and is the equivalent of a primary key. You have not mentioned what client you will be running this in. So it is not possible at this time to come up with a code example that presents this to the user for action.

Related