How do two merge multiple CSV files horizontally with python?

I have several CSV files (~ 10) and you need to merge them horizontally into one file. Each file has the same number of lines (~ 300) and 4 header lines that are not necessarily identical, but should not be combined (just take the header lines from the first CSV file). Tokens in strings are separated by commas without spaces between them.

As python noob, I have no solution, although I am sure there is a simple solution to this problem. Any help is appreciated.

+3
source share
6 answers

CSV csv Python. , , , . - :

import csv
reader = csv.reader(open("some.csv", "rb"))
csvContent = list(reader)

, CSV, ( ):

[ ("header1", "header2", "header3", "header4"),
  ("value01", "value12", "value13", "value14"),
  ("value11", "value12", "value13", "value14"),
  ... 
]

:

result = [a+b for (a,b) in zip(csvList1, csvList2)]

, :

writer = csv.writer(open("some.csv", "wb"))
writer.writerows(result)
+6

csv - .

+2

csv.

file1 = open(file1)

from itertools import izip_longest

foo=[]
for new_line in izip_longest(file1,fil2,file3....,fillvalue=''):
    foo.append(new_line)

( kon ). ,

[ ("line10", "line20", "line30", "line40"),
  ("line11", "line21", "line31", "line41"),
  ... 
]

1

for listx in foo:
    new_file.write(','.join(j for j in listx))

PS: izip_longest

+1

, ( , ). , . :

, , Python 3. ( , Python , , IO .)

0

Python, , paste/gawk ..

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

. , file1

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

Purely for educational purposes

A simple approach that does not use the csv module:

# open file to write
file_to_write = open(filename, 'w')
# your list of csv files
csv_files = [file1, file2, ...] 

headers = True
# iterate through your list
for filex in csv_files:
    # mark the lines that are header lines
    header_count = 0
    # open the csv file and read line by line
    filex_f = open(filex, 'r')
    for line in filex_f:
        # write header only once
        if headers:
            file_to_write.write(line+"\n")
            if header_count > 3: headers = False
        # Write all other lines to the file
        if header_count > 3:
            file_to_write.write(line+"\n")
        # count lines
        header_count = header_count + 1
    # close file
    filex_f.close()
file_to_write.close()
0
source

Source: https://habr.com/ru/post/1770701/


All Articles