How two merge several .csv files horizontally with python?

后端 未结 6 549
渐次进展
渐次进展 2021-01-14 16:11

I\'ve several .csv files (~10) and need to merge them together into a single file horizontally. Each file has the same number of rows (~300) and 4 header lines which are not

6条回答
  •  广开言路
    2021-01-14 16:22

    If you don't necessarily have to use Python, you can use shell tools like paste/gawk etc

    $ paste file1 file2 file3 file4 .. | awk 'NR>4'
    

    The above will put them horizontally without the headers. If you want the headers, just get them from file1

    $  ( head -4 file ; paste file[1-4] | awk 'NR>4' ) > output
    

提交回复
热议问题