Read in all csv files from a directory using Python

后端 未结 4 928
傲寒
傲寒 2020-12-13 10:44

I hope this is not trivial but I am wondering the following:

If I have a specific folder with n csv files, how could I iteratively read all of

4条回答
  •  鱼传尺愫
    2020-12-13 11:16

    According to the documentation of numpy.genfromtxt(), the first argument can be a

    File, filename, or generator to read.

    That would mean that you could write a generator that yields the lines of all the files like this:

    def csv_merge_generator(pattern):
        for file in glob.glob(pattern):
            for line in file:
                yield line
    
    # then using it like this
    
    numpy.genfromtxt(csv_merge_generator('*.csv')) 
    

    should work. (I do not have numpy installed, so cannot test easily)

提交回复
热议问题