简体   繁体   中英

Combine csv files and add header rows - vertically

I have the following code that combines several csv files and adds file names as headers, but adds them horizontally:

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)
                        header[0] = f
                        _extend(lines, 0, header)
                    _extend(lines, lineno, line)

    # print new file
    with open(out_path, 'w', newline='\n') as csvfile:
        csv.writer(csvfile).writerows(lines)


if __name__ == '__main__':
    main()

This achieves the following output: 在此处输入图片说明

But I want it vertically. How can I do this ?

references: this

I managed to merge results vertically using the following script:

def combine_vertically(curr_path, sub):
    with open('%s.csv' % sub, 'w') as wf:
        for subdir, dirss, files in os.walk(curr_path):
            if 'error_metrics_csv' in dirss:
                wf.write("\n")
                wf.write(subdir.split('\\')[-2])
                wf.write("\n")

                # csv_files.append(os.path.join(subdir, 'error_metrics_csv'))
                ptf = os.path.join(subdir, 'error_metrics_csv')
                file = os.path.join(ptf, 'errors.csv')
                with open(file) as rf:
                    for line in rf:
                        if line.strip():  # if line is not empty
                            if not line.endswith("\n"):
                                line = "\n"
                                # line = str(subdir.split('\\')[-1])  "\n"
                            wf.write(line)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM