Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
merging two csv files
#1
so i have this issue where i must make a program where you input the file names which then merge together into a new file keeping the same column names e.g.

A 1
B 2
C 3
D 4

A Monday
B Tuesday
C Wednesday
D Thursday

what i want it to do is:

A 1 Monday
B 2 Tuesday
C 3 Wednesday
D 4 Thursday

what i'm getting is:

[""A","B","C","D""],[""1","2","3","4""],[""Monday","Tuesday","Wednesday","Thursday""]

Here is my code:

import csv
print("Please input the first file location you would like to merge")
#file1 = input()
file1 = ("CLI1.csv")

print("Please now input the second file location")
#file2 = input()
file2 = ("Company1.csv")

rows = []

for f in ("CLI1.csv"):
     readera = csv.reader(open(file1,"r"))

     for row in readera:
           rows.append(row)

for g in ("company1.csv"):
     readerb = csv.reader(open(file2,"r"))

     for row in readerb:
           rows.append(row)
     
rows = str(rows)
     
writer = csv.writer(open("merge.csv", "w"),delimiter=' ')

print("Done")
quit()
does anyone have any suggestions on what i need to change for this to work?

thanks James

here is the other way where it goes

Output:
A B C D 1 2 3 4 M o n d a y T u e s d a y W e d n e s d a y t h u r s d a y
import csv
num = True
print("Please input the first file location you would like to merge")
#file1 = input()
file1 = ("CLI1.csv")

print("Please now input the second file location")
#file2 = input()
file2 = ("Company1.csv")

datadict = []


for f in ("CLI1.csv"):
     readera = csv.reader(open("CLI1.csv","r"))
     for row in readera:
           datadict.append(row)
           
#print(datadict)

for g in ("company1.csv"):
     readerb = csv.reader(open("company1.csv","r"))

     for row in readerb:
           datadict.append(row)

#print(datadict)
datadict = str(datadict)
     
writer = csv.writer(open("merge.csv", "w"),delimiter=' ')
##writer.writerow(''[rows])num=num+1
       
while num==True:
     
     writer.writerow("\n".join(datadict))
else:
     writer.writerow("".join(datadict))


num = False
print("Done")
quit()
Reply


Messages In This Thread
merging two csv files - by jlcolam - Jul-13-2017, 11:05 AM
RE: merging two csv files - by ichabod801 - Jul-13-2017, 12:55 PM
RE: merging two csv files - by jlcolam - Jul-13-2017, 01:45 PM
RE: merging two csv files - by ichabod801 - Jul-13-2017, 06:59 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,184 Feb-21-2023, 04:21 AM
Last Post: Auz_Pete
  Help needed with merging two CSV files eyadfr 5 2,991 Dec-14-2021, 07:34 PM
Last Post: paul18fr
  Sorting and Merging text-files [SOLVED] AlphaInc 10 4,917 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,796 Dec-09-2020, 05:14 PM
Last Post: sutra
  Merging Excel Files JezMim 1 1,907 Sep-06-2020, 08:56 PM
Last Post: bowlofred
  Merging CSV Files in Jupyter RJ117 0 4,774 Jan-07-2018, 06:24 AM
Last Post: RJ117

Forum Jump:

User Panel Messages

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