Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中修改同一目录中的多个.csv文件_Python_Csv - Fatal编程技术网

在python中修改同一目录中的多个.csv文件

在python中修改同一目录中的多个.csv文件,python,csv,Python,Csv,我需要修改目录中的多个.csv文件。可以用一个简单的脚本来完成吗? My.csv列的顺序如下: X_center,Y_center,X_Area,Y_Area,Classification Classification,X_center,Y_center,X_Area,Y_Area 但它将每一行更改为分类,X_中心、Y_中心、X_区域、Y_区域替换每一行中的值。 是否可以打开一个文件,对列重新排序并以相同的名称保存该文件? 我检查了其他线程上给出的类似解决方案,但没有运气。 谢谢你的帮助 使用p

我需要修改目录中的多个.csv文件。可以用一个简单的脚本来完成吗? My.csv列的顺序如下:

X_center,Y_center,X_Area,Y_Area,Classification Classification,X_center,Y_center,X_Area,Y_Area 但它将每一行更改为分类,X_中心、Y_中心、X_区域、Y_区域替换每一行中的值。 是否可以打开一个文件,对列重新排序并以相同的名称保存该文件? 我检查了其他线程上给出的类似解决方案,但没有运气。
谢谢你的帮助

使用pandas和pathlib

from pathlib import Path # available in python 3.4 + 
import pandas as pd
dir = r'c:\path\to\csvs' # raw string for windows.
csv_files = [f for f in Path(dir).glob('*.csv')] # finds all csvs in your folder.


cols = ['Classification','X_center','Y_center','X_Area','Y_Area']

for csv in csv_files: #iterate list
    df = pd.read_csv(csv) #read csv
    df[cols].to_csv(csv.name,index=False)
    print(f'{csv.name} saved.')

当然,如果csv中没有这些列,那么此代码将失败,您可以使用pandas&pathlib添加try/except

from pathlib import Path # available in python 3.4 + 
import pandas as pd
dir = r'c:\path\to\csvs' # raw string for windows.
csv_files = [f for f in Path(dir).glob('*.csv')] # finds all csvs in your folder.


cols = ['Classification','X_center','Y_center','X_Area','Y_Area']

for csv in csv_files: #iterate list
    df = pd.read_csv(csv) #read csv
    df[cols].to_csv(csv.name,index=False)
    print(f'{csv.name} saved.')

当然,如果csv没有这些列,那么此代码将失败,如果是这样,您可以添加try/except。

首先,我认为您的问题在于在循环中打开“*.csv”,而不是打开文件。尽管如此,我还是建议不要覆盖原始输入文件。将副本写入新目录更安全。这是你的脚本的一个修改版本

import os
import csv
import argparse

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--input", required=True)
ap.add_argument("-o", "--output", required=True)
args = vars(ap.parse_args())


if os.path.exists(args["output"]) and os.path.isdir(args["output"]):
        print("Writing to {}".format(args["output"]))
else:
        print("Cannot write to directory {}".format(args["output"]))
        exit()

for file in os.listdir(args["input"]):
    if file.endswith(".csv"):
        print("{} ...".format(file))
        with open(os.path.join(args["input"],file), 'r') as infile, open(os.path.join(args["output"], file), 'w') as outfile:
            fieldnames = ['Classification','X_center','Y_center','X_Area','Y_Area']
            writer = csv.DictWriter(outfile, fieldnames=fieldnames)
            writer.writeheader()
            for row in csv.DictReader(infile):
                writer.writerow(row)
        outfile.close()
要使用它,请为输出创建一个新目录,然后按如下方式运行:

python this.py -i input_dir -o output_dir
注:
从您的问题来看,您似乎希望对每个文件进行适当的修改,这样基本上可以输出相同名称的文件,只是在不同的目录中,但不会损坏您的输入。如果您确实希望按照代码打开“reordered.csv”、“a”的意思将所有文件重新排序到单个文件中,您可以通过移动输出初始化代码来轻松实现这一点,以便在进入循环之前执行它。

首先,我认为您的问题在于在循环中打开“*.csv”,而不是打开文件。尽管如此,我还是建议不要覆盖原始输入文件。将副本写入新目录更安全。这是你的脚本的一个修改版本

import os
import csv
import argparse

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--input", required=True)
ap.add_argument("-o", "--output", required=True)
args = vars(ap.parse_args())


if os.path.exists(args["output"]) and os.path.isdir(args["output"]):
        print("Writing to {}".format(args["output"]))
else:
        print("Cannot write to directory {}".format(args["output"]))
        exit()

for file in os.listdir(args["input"]):
    if file.endswith(".csv"):
        print("{} ...".format(file))
        with open(os.path.join(args["input"],file), 'r') as infile, open(os.path.join(args["output"], file), 'w') as outfile:
            fieldnames = ['Classification','X_center','Y_center','X_Area','Y_Area']
            writer = csv.DictWriter(outfile, fieldnames=fieldnames)
            writer.writeheader()
            for row in csv.DictReader(infile):
                writer.writerow(row)
        outfile.close()
要使用它,请为输出创建一个新目录,然后按如下方式运行:

python this.py -i input_dir -o output_dir
注:
从您的问题来看,您似乎希望对每个文件进行适当的修改,这样基本上可以输出相同名称的文件,只是在不同的目录中,但不会损坏您的输入。如果您确实希望按照代码打开“reordered.csv”、“a”的意思将所有文件重新排序到一个文件中,您可以通过移动输出初始化代码来轻松地做到这一点,以便在进入循环之前执行它。

您同意使用pandas吗?pip安装pandas如果是这样,我将在下面写一个解决方案:你可以使用pandas吗?pip安装熊猫如果是这样,我将在下面编写一个解决方案: