1 Replies - 1597 Views - Last Post: 19 April 2007 - 03:09 AM Rate Topic: -----

#1 sravanth.gurajapu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-April 07

lists in list

Posted 18 April 2007 - 11:58 PM

f = open('output.txt','r')
lines = f.readlines()
print lines
for i in range(0,len(lines)):
	print lines[i]
#print lines
auto = raw_input("Enter account No::")
a=[]
j =[]
count =len(lines)
listSize = lines[count-1]
k= listSize.rstrip(",")
#print type(k)	
print k	
#k.split(",")
#a=k.split(",")[7]
a=k.split(",")
	
#for i in range(0,len(a)):
#	print a[i]	
	
#ab=""
#ab= str(int(a)+1)


this code will return the data in a list format, within a list. the output will look like this
['ds,r,r,2007-04-18,r,r,r,20070213,\n', '\n', 'f,m,45,2007-04-18,gsd,dfg,c,20070214,']
now my requirement is when the user enters a number eg:20070213 then it has to return
['ds,r,r,2007-04-18,r,r,r,20070213']
this 20070213 is an autogenerated number(primary key) with this key u need to retreive the corresponding values. This is like list in a list.

Is This A Good Question/Topic? 0
  • +

Replies To: lists in list

#2 Kiriran  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 60
  • Joined: 11-April 07

Re: lists in list

Posted 19 April 2007 - 03:09 AM

My python is a bit rusty but what you need is a dictionary.
f = open("output.txt")
lines = f.readlines();

mydict = {} #create dictionary

for line in lines:
	key = line.split(",")[7].strip() # get the key
	mydict[key] = line # store in dictionary

lookupkey = raw_input("enter a key: ").strip()
print mydict[lookupkey] #lookup the values for the key

hth
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1