2

I have csv file with many entire blank rows.

Step 1: I follow the first answer in this link Delete blank rows from CSV? to get rid of the blank rows. The code in this link is

with open(in_fnam) as in_file:
    with open(out_fnam, 'w') as out_file:
        writer = csv.writer(out_file)
        for row in csv.reader(in_file):
            if row:
                writer.writerow(row)

My csv file name is "Profit.csv". I wrote my code like this

with open("Profit.csv") as in_file:
    with open("Profit_1.csv", 'w') as out_file:
        writer = csv.writer(out_file)
        for row in csv.reader(in_file):
            if row:
                writer.writerow(row)

Step 2: I then checked the missing values and blank rows in "Profit_1.csv" but they are the same as the missing values and blank rows in "Profit.csv"

QUESTION: What should I do now to get the updated csv file after deleting blank rows? Thanks in advance!

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
MinHaw
  • 39
  • 9
  • Does this answer your question? [Delete blank rows from CSV?](https://stackoverflow.com/questions/4521426/delete-blank-rows-from-csv) – pmcarpan Jan 28 '21 at 11:36
  • No. I read the answer in this link already. I believe this helps deletes blank rows in the original csv file but I don't know to get the updated csv file (the one with blank rows). – MinHaw Jan 28 '21 at 11:40
  • In the first answer in the link, there is a line `with open(out_fnam, 'w') as out_file` which I believe writes the output to a file named by the variable `out_fnam`. Have you checked whether any new file is getting saved? – pmcarpan Jan 28 '21 at 11:43
  • I've checked several times but out_fnam have the same missing values as in_fnam. It seems they are the same file or may be I did something wrong. – MinHaw Jan 28 '21 at 13:32
  • I see. If you edit the question and add in what you have tried, and also add the modified/whole code that you are using, someone may be able to help you better :) – pmcarpan Jan 28 '21 at 13:35
  • Here I understood that in_fnam is my ``FileName.csv" (my original csv file). I wrote out_fnam as ``FileName_1.csv". Then, I checked missing values of ``FileName_1.csv" but they have the same missing values as the original csv file, i.e. ``FileName.csv". – MinHaw Jan 28 '21 at 13:37
  • @pmcarpan Thanks! I do it now. – MinHaw Jan 28 '21 at 13:38

2 Answers2

1

An empty row in a CSV file will result in a row object that is a list of many empty strings (['','','','','']). In this case, if row: returns True.

Change

if row:

to

if any(row):

in order to remove rows that consist solely of empty strings.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • I've just done so but then all missing values are gone. But all values becomes "NaN" and all columns' names become "Unnamed:0, Unnamed:1, ect" – MinHaw Jan 28 '21 at 14:38
  • Ah, of course, my solution was bass-ackwards... please try again – Tim Pietzcker Jan 28 '21 at 14:57
  • 1
    Also, don't forget to use the correct method for opening CSV files (see [docs](https://docs.python.org/3/library/csv.html)). i. e. use the `newline=''` parameter - otherwise you can expect problems with CSV files that contain embedded newlines. – Tim Pietzcker Jan 28 '21 at 15:06
  • Thanks so much, Tim. I believe the problem is solved now. – MinHaw Jan 28 '21 at 15:20
0

Ithink you need to close the file between step 1 and 2

n-klobucar
  • 1
  • 1
  • 1
  • I modified the question a little bit. Could you please take a look my question again and give me a comment? Thanks! – MinHaw Jan 28 '21 at 14:10