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
#2
The zip function should give you what you want:

for row in zip(["A","B","C","D"],["1","2","3","4"],["Monday","Tuesday","Wednesday","Thursday"]):
     print(row)
Output:
('A', '1', 'Monday') ('B', '2', 'Tuesday') ('C', '3', 'Wednesday') ('D', '4', 'Thursday')
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
I put that command in and it gives  it in one row not multiple i may of not put it in the right place
datadict = str(datadict)
     
writer = csv.writer(open("merge.csv", "w"),delimiter=' ',lineterminator='\r\n',quotechar=' ', quoting=csv.QUOTE_MINIMAL)


                   
##writer.writerow(
for row in zip([datadict]):
    print(row)
writer.writerow(datadict)
print("Done")
quit()
and it gave me this

Output:
[A,B,C,D        1,2,3,4         Monday,Tuesday,Wednesday,Thursday]
Reply
#4
Don't make a string out of it first. You make a string out of it, and then zip a list of just the string. Zipping a list of one item does nothing. Zip takes multiple lists and transposes them, so you need to give it multiple lists with multiple items each. Then you print the results rather than writing them to the file. Since datadict is a lists of the lists you want to zip, you need to unpack it with the star operator (*). So remove datadict = str(datadict), and change your for loop to:

for row in zip(*datadict):
    writer.writerow(row)
print('Done')
Note that you don't need an explicit quit() at the end of a program. Finally, since datadict is a list, please don't call it a dict. It makes your code confusing.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Merging multiple csv files with same X,Y,Z in each Auz_Pete 3 1,087 Feb-21-2023, 04:21 AM
Last Post: Auz_Pete
  Help needed with merging two CSV files eyadfr 5 2,865 Dec-14-2021, 07:34 PM
Last Post: paul18fr
  Sorting and Merging text-files [SOLVED] AlphaInc 10 4,755 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,760 Dec-09-2020, 05:14 PM
Last Post: sutra
  Merging Excel Files JezMim 1 1,859 Sep-06-2020, 08:56 PM
Last Post: bowlofred
  Merging CSV Files in Jupyter RJ117 0 4,728 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