Beginners help!

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2009
Posts: 3
Reputation: Ally89 is an unknown quantity at this point 
Solved Threads: 0
Ally89 Ally89 is offline Offline
Newbie Poster

Beginners help!

 
0
  #1
Sep 3rd, 2009
Hi all

I'm needing help with a question in my tutorial due tomorrow...

I'm given this file which contains the following:

toy201, Racing Car, AB239:2,DC202:2,FR277:2,JK201:1,YU201:1,EE201:1,FW201:1
toy101, Barbie Doll, AB139:2,DC102:2,FR177:2,JK101:1,YU101:1,EE101:1,FW101:1
toy301, Flying Kite, AB339:2,DC302:2,FR377:2,JK301:1,YU301:1,EE301:1,FW301:1

I am now to define a function that gives the following from the above file:

>>>load_prods(filename)
{'toy301': ('Flying Kite', [('AB339',2),('DC302',2),('FR377',2),('JK301),1),('YU301',1),('EE301',1),('FW301),1)])

.......(and the same for barbie doll and flying kite)........ }

Any help would be greatly appreciated...
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 99
Reputation: akulkarni is an unknown quantity at this point 
Solved Threads: 4
akulkarni akulkarni is offline Offline
Junior Poster in Training

Re: Beginners help!

 
0
  #2
Sep 3rd, 2009
i find (as far as i saw) u need a change after a comma(,).We will read the file character by character .When we encounter a comma we will go back and print the word..'word',n..something like this
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Beginners help!

 
0
  #3
Sep 3rd, 2009
A nice brain teaser to get to know Python, here are some hints:
  1. """wants dictionary object -->
  2. {'toy301': ('Flying Kite', [('AB339',2),('DC302',2),('FR377',2),
  3. ('JK301),1),('YU301',1),('EE301',1),('FW301),1)])}
  4. """
  5.  
  6. # typical data line from a file
  7. line = """\
  8. toy301, Flying Kite, AB339:2,DC302:2,FR377:2,JK301:1,YU301:1,EE301:1,FW301:1
  9. """
  10.  
  11. # first split the line at the commas
  12. list1 = line.split(",")
  13. print(list1)
  14.  
  15. """result -->
  16. ['toy301', ' Flying Kite', ' AB339:2', 'DC302:2', 'FR377:2',
  17. 'JK301:1', 'YU301:1', 'EE301:1', 'FW301:1\n']
  18. """
  19.  
  20. # next create a list of tuples from items that have ':'
  21. tuple_list = []
  22. for item in list1:
  23. # strip off trailing newline
  24. line = line.rstrip()
  25. if ':' in item:
  26. text, num = item.split(":")
  27. tuple_list.append((text, int(num)))
  28.  
  29. print(tuple_list)
  30.  
  31. """result -->
  32. [(' AB339', 2), ('DC302', 2), ('FR377', 2), ('JK301', 1),
  33. ('YU301', 1), ('EE301', 1), ('FW301', 1)]
  34. """
  35.  
  36. # now you are ready to piece together the dictionary object from
  37. # parts in list1 and the tuple_list
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 3
Reputation: Ally89 is an unknown quantity at this point 
Solved Threads: 0
Ally89 Ally89 is offline Offline
Newbie Poster

Re: Beginners help!

 
0
  #4
Sep 3rd, 2009
Okay so I have:

  1. def load_products(filename):
  2. filename = 'C:\Users\User\Desktop\cssetute\products.txt'
  3. f = open(filename, 'U')
  4. dictionary={}
  5. for line in f:
  6. list1 = line.strip().split(',')
  7. tuple_list=[]
  8. for item in list1:
  9. line = line.rstrip()
  10. if ':' in item:
  11. text, num = item.split(":")
  12. tuple_list.append((text, int(num)))
  13. dictionary[list1[0]] = tuple_list
  14. f.close()
  15.  
  16. return dictionary

But when i load_products('products.txt') it comes up with:

{'toy301': [(' AB339',2),('DC302',2),('FR377',2),('JK301),1),('YU301',1),('EE301',1),('FW301),1)]}

...
The name of the toy is missing and only one line comes up when I need the whole three toy lines to come up.

Help thanks!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,131
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 945
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Beginners help!

 
0
  #5
Sep 4th, 2009
You are almost there, you need to indent properly to keep your statement blocks together ...
  1. def load_products(filename):
  2. #filename = 'C:\Users\User\Desktop\cssetute\products.txt'
  3. f = open(filename, 'r')
  4. dictionary = {}
  5. for line in f.readlines():
  6. list1 = line.strip().split(',')
  7. tuple_list = []
  8. for item in list1:
  9. line = line.rstrip()
  10. if ':' in item:
  11. text, num = item.split(":")
  12. tuple_list.append((text, int(num)))
  13. # build the dictionary
  14. dictionary[list1[0]] = (list1[1], tuple_list)
  15. f.close()
  16. return dictionary
  17.  
  18. my_dict = load_products('products.txt')
  19. print(my_dict)
  20.  
  21. """my output (made somewhat pretty) -->
  22. {'toy301': (' Flying Kite',
  23. [(' AB339', 2), ('DC302', 2), ('FR377', 2), ('JK301', 1),
  24. ('YU301', 1), ('EE301', 1), ('FW301', 1)]),
  25. 'toy201': (' Racing Car',
  26. [(' AB239', 2), ('DC202', 2), ('FR277', 2),
  27. ('JK201', 1), ('YU201', 1), ('EE201', 1), ('FW201', 1)]),
  28. 'toy101': (' Barbie Doll',
  29. [(' AB139', 2), ('DC102', 2), ('FR177', 2),
  30. ('JK101', 1), ('YU101', 1), ('EE101', 1), ('FW101', 1)])}
  31. """
Make sure you don't use tabs for indents!
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,065
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 299
woooee woooee is offline Offline
Veteran Poster

Re: Beginners help!

 
0
  #6
Sep 4th, 2009
Add some print statements to see what is going on
  1. def load_products(filename):
  2. filename = 'C:\Users\User\Desktop\cssetute\products.txt'
  3. f = open(filename, 'U')
  4. dictionary={}
  5. for line in f:
  6. line = line.rstrip()
  7. list1 = line.strip().split(',')
  8. print "list1 created =", list1
  9.  
  10. tuple_list=[]
  11.  
  12. print "searching list1", list1
  13. for item in list1:
  14.  
  15. print " item =", item
  16. if ':' in item:
  17. text, num = item.split(":")
  18. tuple_list.append((text, int(num)))
  19. print " tuple_list =", tuple_list, "\n"
  20.  
  21. dictionary[list1[0]] = tuple_list
  22. print "\ndictionary is now", dictionary, "\n"
  23.  
  24. f.close()
  25.  
  26. return dictionary
And if it easier to understand, use an additional function.
  1. def add_to_dictionary(list_in, dictionary):
  2. tuple_list=[]
  3.  
  4. for item in list_in:
  5. if ':' in item:
  6. text, num = item.split(":")
  7. tuple_list.append((text, int(num)))
  8.  
  9. dictionary[list_in[0]] = tuple_list
  10. return dictionary
  11.  
  12. def load_products(filename):
  13. filename = 'C:\Users\User\Desktop\cssetute\products.txt'
  14. f = open(filename, 'U')
  15. dictionary={}
  16. for line in f:
  17. line = line.rstrip()
  18. list1 = line.strip().split(',')
  19.  
  20. dictionary = add_to_dictionary(list1, dictionary)
  21.  
  22. f.close()
Last edited by woooee; Sep 4th, 2009 at 12:55 am.
Linux counter #99383
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 3
Reputation: Ally89 is an unknown quantity at this point 
Solved Threads: 0
Ally89 Ally89 is offline Offline
Newbie Poster

Re: Beginners help!

 
0
  #7
Sep 4th, 2009
Ahhhh I see, thanks so much everyone! Works perfect now.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 450 | Replies: 6
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC