Dictionaries

ishish ishish at domhain.de
Thu Mar 20 09:11:44 EDT 2014


Hi,

This might sound weird, but is there a limit how many dictionaries a 
can create/use in a single script?

My reason for asking is I split a 2-column-csv (phone#, ref#) file into 
a dict and am trying to put duplicated phone numbers with different ref 
numbers into new dictionaries. The script deducts the duplicated 46 
numbers but it only creates batch1.csv. Since I obviously can't see the 
wood for the trees here, can someone pls punch me into the right 
direction....
...(No has_key is fine, its python 2.7)

f = open("file.csv", 'r')

myDict = {}
Batch1 = {}
Batch2 = {}
Batch3 = {}

for line in f:
	if line.startswith('Number' ):
		print "First line ignored..."
	else:
		k, v = line.split(',')
		myDict[k] = v
f.close()

for k, v in myDict.items():
	if Batch1.has_key(k):
		if k in Batch2.has_key(k):
			Batch3[k] = v
		else:
			Batch2[k] = v
	else:
		Batch1[k] = v

for k, v in Batch1.items():
	newLine = "%s,%s" % (k, v)
	with open("batch1.csv", "a") as f:
		f.write(newLine)

for k, v in Batch2.items():
	newLine = "%s,%s" % (k, v)
	with open("batch2.csv", "a") as f:
		f.write(newLine)

for k, v in Batch3.items():
	newLine = "%s,%s" % (k, v)
	with open("batch3.csv", "a") as f:
		f.write(newLine)



More information about the Python-list mailing list