[Tutor] merging 2 files.

nitin chandra nitinchandra1 at gmail.com
Thu Feb 24 10:54:36 CET 2011


Hello Every One,

I have A problem :)

I have a file with data collected in with 5 colums in a CSV format.
eg. test.csv (below) format
-------------------------------------------------------
26290,75.3186699999993,28.3905499998328,"WELL 3","WELL 3 MENCHOKE FUNCTIOANL"
26290,75.3161800000604,28.3970899999997,"WT 1","WT 1 BHIEND SCHOOL FUNCTIONAL"
26290,75.3162700000357,28.3971299997539,"HP1","HP1 PRIMERY SCHOOL
SHYOPURA NOFUNCTIONAL"
26290,75.3184299998057,28.3902899999283,"GLR 1","GLR1 MENCHOKE FUNCTIONAL"
26290,75.3197099998803,28.3895599995882,"WELL 1","WELL1 BUS STAND NONFUNCTIONAL"
26290,75.3169699997421,28.3956699999652,"TW 1","WELL 2 FRONT OF SCHOOL
NONFUNCTIONAL"
40988,75.269703,28.369377,"HPG1","HPG1 WARD NO. 7"
40988,75.270543,28.368524,"GLR1","GLR1 IN SCHOOL"
40988,75.270429,28.368761,"WT1","WT1 IN SCHOOL"
40988,75.2711484196972,28.3689626934834,"OW1+TW1","OW1+TW1 W. N. 7"
40988,75.271347,28.369323,"GLR1","GLR1 WARD NO. 7"
41458,75.2825099999856,28.4071500000085,"GLR1","GLR1 PO KE PASS"
41458,75.2824,28.40718,"GLR2","GLR2 P.O. KE PASS"
----------------------------------------------

The other file is Jhun.csv

****************************
id,loc_id,habitation_name,source_type,type_habit,location,longitude,latitude,functional_status,operational_status,quality_status,village_quality_status,yield,seasonal_fluctuation,water_fluctuation_min,water_fluctuation_max,avg_depth,para_ph,para_tds,para_cl,para_f,para_no3,bact,rep_status,remark

88075,60942,Raghunathpura,TW3,MH,Madhaya
Mein,,,F,In-Use,Potable,Good,Good,No-Change,0,0,140,8,680,300,1,100,,,remarks4

88074,60942,Raghunathpura,TW2,MH,School Ke
pas,,,F,In-Use,Potable,Good,Good,No-Change,0,0,150,8,620,320,0.5,45,,,remarks3

88073,60942,Raghunathpura,TW1,MH,Harizen basti
,,,F,In-Use,Potable,Good,Good,No-Change,0,0,120,8,810,380,1,45,,,remarks2

88072,60942,Raghunathpura,HpGovt1,MH,Raste Per,,,NF,,,,,,,,,,,,,,,Rep,remarks1

83613,59500,Dhani Hukma,TW3,MH,rasta per,,,NF,,,,,,,,,,,,,,,Non Rep,remarks8

83612,59500,Dhani Hukma,TW2,MH,rasta
per,,,F,In-Use,Potable,Good,Good,No-Change,0,0,140,7.5,660,220,0.5,45,,,remarks7

83611,59500,Dhani Hukma,TW1,MH,rasta
per,,,F,In-Use,Potable,Good,Good,No-Change,0,0,135,7.5,740,180,1,45,,,remarks6

83610,59500,Dhani Hukma,HpGovt5,MH,chowk mai,,,NF,,,,,,,,,,,,,,,Non Rep,remarks5

83609,59500,Dhani Hukma,HpGovt4,MH,chowk mai,,,NF,,,,,,,,,,,,,,,Non Rep,remarks4

83608,59500,Dhani Hukma,HpGovt3,MH,rasta per,,,NF,,,,,,,,,,,,,,,Non Rep,remarks3

83607,59500,Dhani Hukma,HpGovt2,MH,rasta
per,,,F,In-Use,Potable,Good,Good,No-Change,0,0,80,7.5,690,200,0.5,45,,,remarks2

83606,59500,Dhani Hukma,HpGovt1,MH,near ragu
home,,,NF,,,,,,,,,,,,,,,Non Rep,remarks1

1085,11284,Ashok Nagar,HpGovt1,MH,IN SCHOOL,,,NF,,,,,,,,,,,,,,,Rep,

**********************************
PROBLEM : I need to pick the "first coloum" from test.csv AND SEARCH
in jhun.csv "second coloum" , IF matches read that row from jhun.csv,
break it into individual values , concat with the first file,
test.csv, individual values and write to a third file, eg. merged2.csv

currently the data in both the file is 6 - 10,000 rows max.

I am in need of the solution as client breathing down my neck.

this is my 5th attempt.

Thank you VERY much

Nitin

/////////////////////////////////////////
import os, sys, re

f = open ('Jhun_Water_source_details_hab.csv', 'r')
f2 = open ('test.csv', 'r')
fw = f.readline()

# read from Jhun Water csv file
f11 = f.read()
print "This is from Jhun_Water_source_details_hab.csv file"
#print col11 + col12 + col13 + col14
print f11

# read from test csv file
f21 = f2.readline()
ln3 = f21.rstrip('\r\n')
ln4 = ln3.strip('""')
row2 = ln4.split(',')
#print row2
col21 = row2[:1]
col22 = row2[1:2]
col23 = row2[2:3]
print "This is from test.csv file"
#print col21 + col22 + col23
print f21

for line in f11:
	match = None
	if line.find(col21) == 0:
		pass
		if line.find(col21) == f11:
			print line1
			break

////////////////////


More information about the Tutor mailing list