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

问题描述 投票:0回答:1

我正在尝试从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

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

python csv writing
1个回答
0
投票
# 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]])
© www.soinside.com 2019 - 2024. All rights reserved.