使用python将列从CSV复制到CSV

时间:2016-09-23 08:49:19

标签: python

我是python的新手,我想知道是否可以将第一列从.csv文件复制到另一个.csv文件。原因是我有很多.csv而不是手动打开每个.csv而只是复制到一个.csv我想自动执行这一步。

非常感谢!

1 个答案:

答案 0 :(得分:0)

Code Snipet: 您需要配置来源和目的地

import os
from os import listdir
from os.path import isfile, join
import csv

mypath="sourcePath"
newcsv="destination"
# for just files in a directory and not the subDirectory
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]

print(onlyfiles)
for file in onlyfiles:
    if file.endswith(".csv"):
        with open(os.path.join(mypath,file ), newline='') as f:
            reader = csv.reader(f)
            for row1 in reader:

                out = csv.writer(open(newcsv,"w"), delimiter=',',quoting=csv.QUOTE_ALL)
            out.writerow(row1);