【发布时间】:2019-10-19 16:13:18
【问题描述】:
我正在尝试从包含一些关于文本的数据的 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 个文档中获取的信息写入不同的列。
【问题讨论】:
-
你可以像这样打开作家
with open(newfile, "w") as wcsvfile: writer = csv.writer(wcsvfile) for row on reader: writer.writerow([row[2]])