Read Multiple Csv Data Files And Sort The Data Into A New Csv File
I am trying to read through multiple csv files in a folder, extract three columns (key_resp.rt, key_resp_4.rt, and participant) from each csv file and write these information in a
Solution 1:
While it would be possible to open the output file multiple times and add data to it (by using a mode
argument of 'a'
for "append" instead of "w"
for normal (over) writing—see the open()
function documentation—in this case it would be less awkward to just open the output file once and leave it that way while appending data to it from the input files, one-by-one.
This is what I mean: (Note: no attempt was made to sort the data because you haven't specified how you wanted that done.)
import csv
import glob
import os
path = r'.\_reading data-2'# Path of directory containing data files.
extension = 'csv'
fieldnames = 'key_resp.rt', 'key_resp_4.rt', 'participant'
output_filename = 'sort.csv'# Output filename.
output_filepath = os.path.join(path, output_filename) # Output in same directory.
csvlist = [filename for filename in glob.glob(os.path.join(path, f'*.{extension}'))
if filename != output_filepath] # Avoid reading any existing output file.print(csvlist) # Print out the list of csv file names.withopen(output_filepath, 'w', newline='') as sortfile:
csv_writer = csv.DictWriter(sortfile, fieldnames=fieldnames, delimiter=',',
extrasaction='ignore')
csv_writer.writeheader()
for file in csvlist:
withopen(file, 'r', newline='') as csvfile:
csv_writer.writerows(csv.DictReader(csvfile))
print('Done')
Post a Comment for "Read Multiple Csv Data Files And Sort The Data Into A New Csv File"