vendredi 8 juillet 2016

Python merge CSV files and append column with file names

I'm merging two CSV files with identical column names and appending a new column to the merged result that lists the filename where each row comes from.

myFileA.csv
Col_A, Col_B

myFileB.csv
Col_A, Col_B

merged.csv
Col_A, Col_B, filename

What I wrote works fine, but since I'm very new to Python, I'm wondering if there's a better and smarter way to write this.

            import csv, os

            def list(*files):
                for f in files:
                    global fname
                    fname = os.path.basename(f)
                    with open(f) as fobj:
                        next(fobj)
                        for line in fobj:
                            yield line

            writer = csv.writer(open('merged.csv', 'wb'))
            writer.writerow(['Col_A', 'Col_B', 'filename'])
            for row in csv.reader(list('myfileA.csv', 'myfileB.csv')):
                row.append(fname)
                writer.writerow(row)

Aucun commentaire:

Enregistrer un commentaire