Python Forum
Help needed with merging two CSV files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help needed with merging two CSV files
#1
Hello,

I need your help with the below please 🙏

I have TWO ".CSV" files with the same number of columns and rows (257 columns and X rows). I want to merge them together as follows;

Write the content of the first (256 columns) from the first file, then write the content of the second file.

I have written below code, but I'm ending up with a loop that creates a huge size huge that crashes my computer every time! Although, when testing with a smaller size CSV files, I'm getting a good result.

# This is a sample Python script.

import csv

# read data from file1.csv and file.csv
file1 = open('./File1.csv')
file2 = open('./File2.csv')

# make csv readers object for each file
reader1 = csv.reader(file1)
reader2 = csv.reader(file2)

# write a result to result.csv
file3 = open('./Result.csv', 'w')
writer = csv.writer(file3)

dic = {}
words = []

# read data from file1
for row in reader1:
    count = len(row)
    words.append(row[count - 1])
    dic[row[count - 1]] = row[0:count - 1]

# read data from file2 and merge data with file1
for row in reader2:
    count = len(row)
    dic[row[count - 1]] += row[0:count - 1]

#write data to result.csv
for word in words:
    dic[word].append(word)
    writer.writerow(dic[word])

print('merge finished')
Reply


Messages In This Thread
Help needed with merging two CSV files - by eyadfr - Dec-13-2021, 10:51 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Merging multiple csv files with same X,Y,Z in each Auz_Pete 3 1,180 Feb-21-2023, 04:21 AM
Last Post: Auz_Pete
  Sorting and Merging text-files [SOLVED] AlphaInc 10 4,909 Aug-20-2021, 05:42 PM
Last Post: snippsat
  Merging all file_name.log's files from directory to one and search “PerformanceINFO" sutra 0 1,794 Dec-09-2020, 05:14 PM
Last Post: sutra
  Merging Excel Files JezMim 1 1,905 Sep-06-2020, 08:56 PM
Last Post: bowlofred
  Merging CSV Files in Jupyter RJ117 0 4,770 Jan-07-2018, 06:24 AM
Last Post: RJ117
  merging two csv files jlcolam 3 11,417 Jul-13-2017, 06:59 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020