PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 29, 2022

[FIXED] How to delete a row in a CSV file if a cell is empty using Python

 August 29, 2022     csv, python     No comments   

Issue

I want to go through large CSV files and if there is missing data I want to remove that row completely, This is only row specific so if there is a cell that = 0 or has no value then I want to remove the entire row. I want this to happen for all the columns so if any column has a black cell it should delete the row, and return the corrected data in a corrected csv.

import csv

with open('data.csv', 'r') as csvfile:
    csvreader = csv.reader(csvfile)
    for row in csvreader:
        print(row)
        if not row[0]:
             print("12")

This is what I found and tried but it doesnt not seem to be working and I dont have any ideas about how to aproach this problem, help please?

Thanks!


Solution

Due to the way in which CSV reader presents rows of data, you need to know how many columns there are in the original CSV file. For example, if the CSV file content looks like this:

1,2
3,
4

Then the lists return by iterating over the reader would look like this:

['1','2']
['3','']
['4']

As you can see, the third row only has one column whereas the first and second rows have 2 columns albeit that one is (effectively) empty.

This function allows you to either specify the number of columns (if you know them before hand) or allow the function to figure it out. If not specified then it is assumed that the number of columns is the greatest number of columns found in any row.

So...

import csv

DELIMITER = ','

def valid_column(col):
    try:
        return float(col) != 0
    except ValueError:
        pass
    return len(col.strip()) > 0


def fix_csv(input_file, output_file, cols=0):
    if cols == 0:
        with open(input_file, newline='') as indata:
            cols = max(len(row) for row in csv.reader(indata, delimiter=DELIMITER))
    with open(input_file, newline='') as indata, open(output_file, 'w', newline='') as outdata:
        writer = csv.writer(outdata, delimiter=DELIMITER)
        for row in csv.reader(indata, delimiter=DELIMITER):
            if len(row) == cols:
                if all(valid_column(col) for col in row):
                    writer.writerow(row)

fix_csv('original.csv', 'fixed.csv')


Answered By - Vlad
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing