Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8590585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:19:38+00:00 2026-06-11T23:19:38+00:00

I am reading a csv into a list, and I need to delete an

  • 0

I am reading a csv into a list, and I need to delete an entire line from the list under a specific condition:

import csv

thefile = []

def dowork():
    sourceFile='e.csv'
    CleanFile(sourceFile)

def CleanFile(sourceFile):
    global thefile
    thefile=list(csv.reader(open(sourceFile, 'rb'), delimiter=',', quotechar='"'))
    for line in thefile:
        if line[3]=='':
            #here's where i need to delete line from thefile

how would i delete line from thefile?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:19:39+00:00Added an answer on June 11, 2026 at 11:19 pm

    You need the index of line within thefile. Once you have that, it’s easy. And the way you get that is with enumerate.

    def CleanFile(sourceFile):
        global thefile
        thefile=list(csv.reader(open(sourceFile, 'rb'), delimiter=',', quotechar='"'))
        for i, line in enumerate(thefile):
            if line[3]=='':
                del thefile[i]
    

    However, it’s worth asking whether you actually need to delete it. For example, could you just filter it out like this?

    def CleanFile(sourceFile):
        global thefile
        thefile=[line for line in 
                 csv.reader(open(sourceFile, 'rb'), delimiter=',', quotechar='"')) 
                 if line[3] != '']
    

    Or, depending on what you’re doing with the data, maybe it makes even more sense to just skip over lines where line[3] == '' at output or calculation time. Or, even better, don’t make a list in the first place; just leave csv.reader as an iterator—then you can stick a filter in front of it via itertools.ifilter, or a generator expression. In fact, that’s as simple as just changing two characters:

    def CleanFile(sourceFile):
        global thefile
        thefile=(line for line in 
                 csv.reader(open(sourceFile, 'rb'), delimiter=',', quotechar='"')) 
                 if line[3] != '')
    

    Of course this makes no sense if you need, e.g., random access to thefile. But if you’re going to iterate over it line by line, this is probably the best solution. (Except that thefile shouldn’t be a global; it should be passed around from function to function as needed.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am reading a csv file into a list of a list in python.
I am reading a csv file into a variable data like this: def get_file(start_file):
i am reading a csv file into a data: def get_file(start_file): #opens original file,
I need to delete some rows from csv file based on a column value.
I need to import a large CSV file into an SQL server. I'm using
I am reading a csv into a : import csv import collections import pdb
There are many libraries for reading/writing/appending text from/into a csv file in java. Which
I've got a bunch of csv files that I'm reading into R and including
I'm reading a .csv file that was created in Excel with the first line
I'm reading in a csv file from a datatable and something is happening. Here's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.