读取多个csv数据文件,将数据整理成一个新的csv文件 [英] Read multiple csv data files and sort the data into a new csv file

查看:106
本文介绍了读取多个csv数据文件,将数据整理成一个新的csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取文件夹中的多个 csv 文件,提取三列(key_resp.rtkey_resp_4.rtparticipant>) 从每个 csv 文件,并将这些信息写入一个新的 csv 文件.我能够在不将结果写入 csv 文件的情况下运行 for 循环.但是使用下面的代码,新的 csv 文件 (sort.csv) 只包含数据字符串的标题,而没有真正的数据.

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 new csv file. I am able to get the for loop running without write the result into the csv files. But with the code below, the new csv file (sort.csv) only contains the header of the data strings without the real data.

我的代码:

import os
import glob
import csv

#path contain the directory of the folder
path = r'C:\Users\Time estimates task\modified psychopy\reading data-2'
extension = 'csv'
os.chdir(path)
csvlist = glob.glob('*.{}'.format(extension))
print(csvlist) #print out the list of csv file names
for file in csvlist:
    with open (file,'r') as csvfile:
        csv_reader=csv.DictReader(csvfile)
#        for line in csv_reader:
#            print (line['participant'])
        with open('sort.csv','w') as sortfile:
            fieldnames=['key_resp.rt','key_resp_4.rt', 'participant']
            csv_writer=csv.DictWriter(sortfile,fieldnames=fieldnames,delimiter=',',extrasaction='ignore')
            csv_writer.writeheader()
            for line in csv_reader:
                csv_writer.writerow(line)

推荐答案

虽然可以多次打开输出文件并向其中添加数据(通过使用 mode 用于追加"的 'a' 参数,而不是用于正常(覆盖)写入的 w" — 参见 open() 函数文档——在这种情况下,将不那么尴尬只需打开输出文件一次并保持原样,同时将输入文件中的数据逐一添加.

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.

这就是我的意思:(注意:没有尝试对数据进行排序,因为您没有指定您希望如何完成.)

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.

with open(output_filepath, 'w', newline='') as sortfile:
    csv_writer = csv.DictWriter(sortfile, fieldnames=fieldnames, delimiter=',',
                                extrasaction='ignore')
    csv_writer.writeheader()
    for file in csvlist:
        with open(file, 'r', newline='') as csvfile:
            csv_writer.writerows(csv.DictReader(csvfile))

print('Done')

这篇关于读取多个csv数据文件,将数据整理成一个新的csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆