943,840 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 698
  • Python RSS
Sep 3rd, 2009
0

Beginners help!

Expand Post »
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...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ally89 is offline Offline
3 posts
since Sep 2009
Sep 3rd, 2009
0

Re: Beginners help!

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
Reputation Points: 11
Solved Threads: 4
Junior Poster
akulkarni is offline Offline
111 posts
since Jun 2009
Sep 3rd, 2009
0

Re: Beginners help!

A nice brain teaser to get to know Python, here are some hints:
python Syntax (Toggle Plain Text)
  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
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Sep 3rd, 2009
0

Re: Beginners help!

Okay so I have:

python Syntax (Toggle Plain Text)
  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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ally89 is offline Offline
3 posts
since Sep 2009
Sep 4th, 2009
0

Re: Beginners help!

You are almost there, you need to indent properly to keep your statement blocks together ...
python Syntax (Toggle Plain Text)
  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!
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Sep 4th, 2009
0

Re: Beginners help!

Add some print statements to see what is going on
Python Syntax (Toggle Plain Text)
  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.
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,305 posts
since Dec 2006
Sep 4th, 2009
0

Re: Beginners help!

Ahhhh I see, thanks so much everyone! Works perfect now.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ally89 is offline Offline
3 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Handling CD Drives from python
Next Thread in Python Forum Timeline: A list of variables?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC