Python 写入CSV文件选择要写入的列

Python 写入CSV文件选择要写入的列,python,csv,writing,Python,Csv,Writing,我试图从包含一些文本数据的X(本例中为6)CSV文件中导入数据,并将每个文档中的一行放到一个新的行上,以使它们彼此相邻(从第1列的文档1导出,从第2行的第二个文档导出,依此类推)。到目前为止我一直不成功 # I have a list containing the path to all relevant files files = ["path1", "path2", ...] # then I tried cycling through the folder lik

我试图从包含一些文本数据的X(本例中为6)CSV文件中导入数据,并将每个文档中的一行放到一个新的行上,以使它们彼此相邻(从第1列的文档1导出,从第2行的第二个文档导出,依此类推)。到目前为止我一直不成功

    # I have a list containing the path to all relevant files
    files = ["path1", "path2", ...]

    # then I tried cycling through the folder like this
    for file in files:
        with open(file, "r") as csvfile:
            reader = csv.reader(csvfile, delimiter=",")
            for row on reader:
                # I'm interested in the stuff stored on Column 2
                print([row[2]])
    # as you can see, I can get the info from the files, but from here 
    # on, I can't find a way to then write that information on the 
    # appropiate coloumn of the newly created CSV file

编辑:我知道如何打开writer,我不知道的是如何编写脚本,在每次处理新文件时将从原始6个文档中获取的信息写入另一列。

您可以像这样打开writer(newfile,“w”)作为wcsvfile:writer=csv.writer(wcsvfile)打开(newfile),
对于reader:writer.writerow([row[2]])
上的行,这不会为我处理的每个文件创建一个新文件吗?我更希望创建一个文件,在其中存储所有内容。问题是,如果我只是在上面添加一个write()函数,脚本就会用新信息覆盖旧信息。使用open(newfile,“a”)将附加数据。不会超车
# I have a list containing the path to all relevant files
files = ["path1", "path2", ...]
newfile = "newpath1"

# then I tried cycling through the folder like this
for file in files:
    with open(file, "r") as csvfile:
        reader = csv.reader(csvfile, delimiter=",")
        with open(newfile, "a") as wcsvfile:
            writer = csv.writer(wcsvfile)
            for row on reader:
                # I'm interested in the stuff stored on Column 2
                writer.writerow([row[2]])