1

I have a list of pathnames to CSV files. I need to open each CSV file, take the data without the header and then merge it all together into a new CSV file.

I have this code which gets me the list of CSV file pathnames:

file_list = []
for folder_name, sub_folders, file_names in os.walk(wd):
    for file_name in file_names:
        file_extention = folder_name + '\\' + file_name
        if file_name.endswith('csv'):
            file_list.append(file_extention)

An example of my list is:

['C:\\Users\\Documents\\GPS_data\\West_coast\\Westland\\GPS_data1.csv',
 'C:\\Users\\Documents\\GPS_data\\West_coast\\Westland\\GPS_data2.csv',
 'C:\\Users\\Documents\\GPS_data\\West_coast\\Westland\\GPS_data3.csv']

I am struggling to figure out what to do, any help would be greatly appreciated. Thanks.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
kels
  • 23
  • 4

1 Answers1

2

The main idea is to read in each line of a file, and write it to the new file. But remember to skip the first line that has the column headers in it. I previously recommend the cvs module, however it doesn't seem like that is necessary, since this task does not require analyzing the data.

file_list = ['data1.csv','data2.csv']

with open('new.csv', 'w') as newfile:  # create a new file
    for filename in filelist:
        with open(filename) as csvfile:
            next(csvfile)   # skip the header row
            for row in csvfile:
                newfile.write(line) # write to the new csv file

Edit: clarified my answer.

Rusty
  • 1,095
  • 9
  • 15