Ad

Removing Second Row Of Csv File And Modify The Columns Using Python

- 1 answer

I am currently use the below-provided code to copy the CSV files from a folder to another and remove the second row of the CSV file present in it.

The above code removes the second row, i.e., row 1, from all the CSV files, but at the same time, it adds an empty row after each row in all the CSV files.

Example - Actual CSV:

| row_no | name | test  |
|        |      |       |
|        |      | input |
-------------------------
|   0    | abc  | 123   |
|   1    | def  | 456   |
|   2    | ghi  | 789   |
|   3    | jkl  | 101   |

After applying code:

| row_no | name | test  |
|        |      |       |
|        |      | input |
-------------------------
|   0    | abc  | 123   |
|        |      |       |
|   2    | ghi  | 789   |
|        |      |       |
|   3    | jkl  | 101   |

Question 1: How do I remove the empty rows?

Question 2: I would like to remove the empty spaces in the column names;

Column Example:

| test  |
|       |  -> |testinput|
| input |

Thanks

Ad

Answer

Q1: By default, csv.writer writes CSV files in the "excel" dialect, which means line endings are \r\n. Try setting csv.writer into "unix" mode, which has line endings that are plain \n.

Q2: Simply use str.replace to replace all spaces with nothing.

Here is a modified version of your code:

import glob, os, shutil, csv

source_path = "/home/Desktop/Output/"
dest_path = "/home/Desktop/Output2/"
file_read_path = "/home/Desktop/Output2/*.csv"

## Code to copy .csv files from one folder to another
for csv_file in glob.iglob(os.path.join(source_path, "*.csv"), recursive = True):
    shutil.copy(csv_file, dest_path)

## Code to delete the second row in all .CSV files
for filename in glob.glob(file_read_path):
    with open(filename, "r") as file:
        reader = list(csv.reader(file , delimiter = ","))
        # Remove second row
        del reader[2]  # 0=columnnames, 1=first data row, 2=second data row
        # Remove spaces from column names
        reader[0] = [colname.replace(" ", "") for colname in reader[0]]
    with open(filename, "w") as output:
        # Set csv.writer to output files in the "unix" dialect
        writer = csv.writer(output, delimiter = ",", dialect="unix")
        for row in reader:
            writer.writerow(row)
Ad
source: stackoverflow.com
Ad