这将垂直连接所提供目录中的所有文件(因此您不必在代码中指定它们)。文件可以有任意数量的列,并且可以处理值中的空格。但是这些文件的行数必须相同。在
它只使用csv和os模块。在import os
import csv
dir_base = r'H:\apps\xp\Desktop\localrepo\Temp'
dir_name = '-test2'
output_name = 'output.csv'
path = os.path.join(dir_base, dir_name)
out_path = os.path.join(dir_base, output_name)
def _extend(lines, lineno, line):
try:
lines[lineno].extend(line)
except IndexError:
lines.append(line)
def main():
lines = []
# read and generate new file
for root, dirs, files in os.walk(path):
for f in files:
with open(os.path.join(root, f), 'r') as csvfile:
f_in = csv.reader(csvfile)
for lineno, line in enumerate(f_in, start=1):
if lineno == 1:
header = [''] * len(line)