Appending two CSV files column-wise

前端 未结 2 805
日久生厌
日久生厌 2021-01-27 08:38

Suppose I have two CSV files called A and B in Python.

A\'s head looks like:

 headerName         


        
2条回答
  •  死守一世寂寞
    2021-01-27 09:00

    You might be able to get away with a named pipe. You have a Python process run which creates a pipe and opens it in write mode. It then outputs to that the column wise concatenation of the CSV files (similar to what you've got) already... When another process starts reading that file, it'll be able to consume the data, but no file is actually stored on the server, it's just on demand. When the "file" is consumed, then there'll be nothing in it, and any attempt to access it will block until another process writes to the other end.

    Some dummy code - will need more thought out exception handling etc...:

    import os
    from itertools import izip
    
    a = 'abcdef' # File A's rows
    b = 'ghijkl' # File B's rows
    
    outname = 'joined'
    
    try:
        os.unlink(outname)
        os.mkfifo(outname)
    except OSError:
        pass
    
    with open(outname, 'w') as fout:
        for items in izip(a, b):
            fout.write(''.join(items) + '\n') # Do "real" write here instead...
        os.unlink(outname)
    

    Something else opens that "file" in read mode and consumes it to retrieve the data. This should work unless that process has to have "physical files"...

提交回复
热议问题