简体   繁体   English

删除csv文件的第二行并使用Python修改列

[英]Removing second row of csv file and modify the columns using Python

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.我目前使用下面提供的代码将 CSV 文件从一个文件夹复制到另一个文件夹,并删除其中存在的 CSV 文件的第二行。

The above code removes the second row, ie, 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.上面的代码从所有CSV文件中删除了第二行,即第1行,但同时,它在所有CSV文件中的每一行之后添加了一个空行。

Example - Actual CSV:示例- 实际 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?问题 1:如何删除空行?

Question 2: I would like to remove the empty spaces in the column names;问题2:我想去掉列名中的空格;

Column Example:列示例:

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

Thanks谢谢

Q1: By default, csv.writer writes CSV files in the "excel" dialect, which means line endings are \\r\\n . Q1:默认情况下, csv.writer以“excel”方言写入 CSV 文件,这意味着行结尾是\\r\\n Try setting csv.writer into "unix" mode, which has line endings that are plain \\n .尝试将csv.writer设置为"unix"模式,该模式的行尾是普通的\\n

Q2: Simply use str.replace to replace all spaces with nothing. Q2:只需使用str.replace将所有空格替换为str.replace

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM