2

I am compiling a load of CSVs into one. The first CSV contains the headers, which I am opening in write mode (maincsv). I am then making a list of all the others which live in a different folder and attempting to append them to the main one.

It works, however it just writes over the headings. I just want to start appending from line 2. I'm sure it's pretty simple but all the next(), etc. things I try just throw errors. The headings and data are aligned if that helps.

import os, csv

maincsv = open(r"C:\Data\OSdata\codepo_gb\CodepointUK.csv", 'w', newline='')
maincsvwriter = csv.writer(maincsv)
curdir = os.chdir(r"C:\Data\OSdata\codepo_gb\Data\CSV")
csvlist = os.listdir()
    csvfiles = []
    for file in csvlist:
        path = os.path.abspath(file)
        csvfiles.append(path)

for incsv in csvfiles:
    opencsv = open(incsv)
    csvreader = csv.reader(opencsv)
    for row in csvreader:
        maincsvwriter.writerow(row)

maincsv.close()
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Elnombre
  • 31
  • 2

1 Answers1

1

To simplify things I have the code load all the files in the directory the python code is run in. This will get the first line of the first .csv file and use it as the header.

import os
count=0
collection=open('collection.csv', 'a')
files=[f for f in os.listdir('.') if os.path.isfile(f)]
for f in files:
    if ('.csv' in f):
        solecsv=open(f,'r')
        if count==0:
            # assuming header is 1 line
            header=solecsv.readline()
            collection.write(header)
        for x in solecsv:
            if not (header in x):
                collection.write(x)
collection.close()