使用python将列从CSV复制到CSV
我是python新手,我想知道是否可以将第一列从.csv文件复制到另一个.csv文件。原因是我有很多.csv文件,我不想手动打开每一个文件,只复制到一个.csv文件,而是想自动化这一步使用python将列从CSV复制到CSV,python,Python,我是python新手,我想知道是否可以将第一列从.csv文件复制到另一个.csv文件。原因是我有很多.csv文件,我不想手动打开每一个文件,只复制到一个.csv文件,而是想自动化这一步 多谢各位 代码Snipet: 您需要配置源和目标 import os from os import listdir from os.path import isfile, join import csv mypath="sourcePath" newcsv="destination" # for just fi
多谢各位 代码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);
您可以用任何可以读取文件的语言来实现这一点。Python为CSV解析提供了特殊的库支持——google itIt是绝对可能的,有很多方法可以做到这一点。查看标准库中的
csv
模块以开始。如果你在实现某些东西时遇到困难,请在这里发布一个特定的问题。你是否曾经尝试过用谷歌搜索它?