0

I want to join two csv files into one like this:

**file 1:**
feb,55,1.23,..,..,0
mar,65,2.33,..,..,1

**file 2:**
feb,55,..,12,KL,..
mar,65,..,10,MN,.. 

so the output would be something like this:

feb,55,1.23,12,KL,0
mar,65,2.33,10,MN,1

My following code snippet doesn't works:

f1=[li.split(',') for li in open("file1.csv","r+")]
f2=[lj.split('\t') for lj in open("file2.csv","r+")]

def joinL(x,y):
    list=[]
    for n in x:
        for m in y:
            if n[0]==m[0]:
                list.append(m)
    return list

print joinL(f1,f2)

Could you please help Thanks!

Oxygenex
  • 5
  • 4
  • Maybe this help: http://stackoverflow.com/questions/16265831/merging-2-csv-files – ρss Apr 17 '14 at 08:25
  • If I understand correctly in one case you have "1.23" and in the other "2" and "33" in the same spot after fussion and I assume that is not know when this happens. Is there are chance that "2","33" is actually "2.33" – Ivaylo Apr 17 '14 at 08:33
  • Yeaaah, I am sorryyy, it s 2.33 – Oxygenex Apr 17 '14 at 09:03

1 Answers1

1

This works for me:

with open('filename1', 'r') as fl1:
    f1 = [i.split(',') for i in fl1.read().split('\n')]

with open('filename2', 'r') as fl2:
    f2 = [i.split(',') for i in fl2.read().split('\n')]

f3 = [[a if b is None or b==len(b)*b[0] else b for a,b in map(None,x,y)] for x,y in zip(f1,f2)]

for i in f3:
    for j in i:
        print j,
    print

[OUTPUT]
feb,55,1.23,12,KL,0
mar,65,2.33,10,MN,1

Note, you had a minor mistake in your text. It should be 2.33 not 2,33.

Here is EXACTLY the code I am using:

#my_script.py
with open('t1.txt', 'r') as fl1:
    f1 = [i.split(',') for i in fl1.read().split('\n')]

with open('t2.txt', 'r') as fl2:
    f2 = [i.split(',') for i in fl2.read().split('\n')]

f3 = [[a if b is None or b==len(b)*b[0] else b for a,b in map(None,x,y)] for x,y in zip(f1,f2)]

for i in f3:
    for j in i:
        print j,
    print

#t1.txt
feb,55,1.23,..,..,0
mar,65,2.33,..,..,1

#t2.txt
feb,55,..,12,KL,..
mar,65,..,10,MN,..
sshashank124
  • 31,495
  • 9
  • 67
  • 76
  • Is it a mistake is still a question :-) – Ivaylo Apr 17 '14 at 08:35
  • it gaves me IndexError: string index out of range – Oxygenex Apr 17 '14 at 09:26
  • @Doublexo, It works fine for me. Are you sure you didn't change any of the code? – sshashank124 Apr 17 '14 at 09:30
  • This is exactly what I wrote: with open('log1.csv', 'r') as fl1: f1 = [i.split(',') for i in fl1.read().split('\n')] with open('log2.csv', 'r') as fl2: f2 = [i.split(',') for i in fl2.read().split('\n')] j=[[a if b is None or b==len(b)*b[0] else b for a,b in map(None,x,y)] for x,y in zip(f1,f2)] for k in j: for l in k: print l print – Oxygenex Apr 17 '14 at 09:35
  • @Doublexo, Did you make note of the error you had in your text file, I have shown in my answer. Also, did you replace, the two filenames above with your appropriate filenames? – sshashank124 Apr 17 '14 at 09:37
  • and here is the error: j=[[a if b is None or b==len(b)*b[0] else b for a,b in map(None,x,y)] for x,y in zip(f1,f2)] IndexError: string index out of range – Oxygenex Apr 17 '14 at 09:37
  • yeaah I did exactly what you said and my files are log1.csv and log2.csv – Oxygenex Apr 17 '14 at 09:38
  • Could you please explain to me what does b==len(b)*b[0] mean?? THANKS – Oxygenex Apr 18 '14 at 15:43