Skip to content

Instantly share code, notes, and snippets.

@raviprakashgiri
Last active January 25, 2016 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raviprakashgiri/e775a6abdf556e96e8f4 to your computer and use it in GitHub Desktop.
Save raviprakashgiri/e775a6abdf556e96e8f4 to your computer and use it in GitHub Desktop.
Script to extract value corresponding to one column in file 1 from two other files.
# Input files from which first 5 columns have to be retained: input0.csv
# Input files from which column's content have to be copied: input1,input2, input3.csv
# It searches value in other three columns from csv files, corresponding to column value in input0.csv
# Author Ravi Prakash Giri
import os
file0=open('input0.csv','r')
file1=open('input1.csv','r')
file2=open('input2.csv','r')
file3=open('input3.csv','r')
file4=open('output742.csv','w')
print file0.readline()
print file1.readline()
print file2.readline()
print file3.readline()
input0=[]
input1=[]
input2=[]
input3=[]
for line in file0:
input0.append([i for i in line.split(',') if i])
for line in file1:
input1.append([i for i in line.split(',') if i])
for line in file2:
input2.append([i for i in line.split(',') if i])
for line in file3:
input3.append([i for i in line.split(',') if i])
reslut=[]
for keyline in input0:
key=keyline[1]
r1='-'
r2='-'
r3='-'
tmp=[i.strip('\r\n') for i in keyline[1:6]]
ln=len(tmp)
#print ln,ln
if (5-ln):
i=0
while i<(5-ln):
#print 'in'
i=i+1
tmp.append('-')
#print "->",len(tmp)
for line in input1:
if key==line[2]:
r1=line[-1].strip('\r\n')
for line in input2:
if key==line[2]:
r2=line[-1].strip('\r\n')
for line in input3:
if key==line[2]:
r3=line[-1].strip('\r\n')
reslut.append(tmp+[r1,r2,r3])
for line in reslut:
file4.write(str(line).strip('[]')+'\n')
file4.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment