combining one-column text files into one multi-column file, separated by tabs

后端 未结 2 1008
别跟我提以往
别跟我提以往 2021-01-26 04:39

I have several one-column text files that I want to put next to each other (e.g. each file is one column). The only documentation I could find was on appending files to the bott

2条回答
  •  终归单人心
    2021-01-26 05:22

    Here's a good way to do it incrementally and makes sure all the files involved are closed when it ends:

    from contextlib import contextmanager
    import glob
    from itertools import izip
    
    @contextmanager
    def multi_file_manager(files, mode='rb'):
        """ Open multiple files and make sure they all get closed. """
        files = [open(file, mode) for file in files]
        yield files
        for file in files:
            file.close()
    
    def read_info(file):
        """ Generator function to read and extract info from each line of a file. """
        for line in file:
            yield line.split('[')[0]
    
    with open("MA_continuous_results.csv", "wb") as outfile:
        MA_files = glob.glob("MA*continuous*2")
    
        col_headers = (filename.split("maskave_")[-1] for filename in MA_files)
        outfile.write('\t'.join(col_headers) + '\n')
    
        with multi_file_manager(MA_files) as infiles:
            generators = [read_info(file) for file in infiles]
    
            for fields in izip(*generators):
                outfile.write('\t'.join(fields) + '\n')
    

提交回复
热议问题