944,066 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 344
  • Python RSS
Oct 25th, 2009
0

Strange outcome using enumerate()

Expand Post »
I have to make some bookmarks so I quickly knocked up the script below and ran it. It created the files fine but with a with an incorrect ip in the second file

The second bookmark created should be named York.URL which it is but the ip address of the url is 192.65.0.95 where it should be 192.65.2.95. It uses the enumerate value and adds 1 each time.

I'd imagine I'd be able to get the desired outcome by using other methods but what is going on here? Am I missing something obvious?

Python Syntax (Toggle Plain Text)
  1. from __future__ import print_function
  2.  
  3. shops = [ 'Windermere',
  4. 'York',
  5. 'Chester' ]
  6.  
  7. for i, s in enumerate(shops):
  8. f = open('%s.URL' % (s), 'w')
  9. f.write('[InternetShortcut]\nURL=http://192.65.%s.95/BackO' % (i+1))
  10. f.close()
  11. print('%s completed' % s)
Last edited by dads; Oct 25th, 2009 at 2:20 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
dads is offline Offline
5 posts
since Jun 2009
Oct 25th, 2009
0
Re: Strange outcome using enumerate()
This little test code works fine ...
python Syntax (Toggle Plain Text)
  1. shops = [ 'Windermere',
  2. 'York',
  3. 'Chester' ]
  4.  
  5. for i, s in enumerate(shops):
  6. print(i, s)
  7. print('[InternetShortcut]\nURL=http://192.65.%s.95/BackO' % (i+1))
  8.  
  9. """my result -->
  10. (0, 'Windermere')
  11. [InternetShortcut]
  12. URL=http://192.65.1.95/BackO
  13. (1, 'York')
  14. [InternetShortcut]
  15. URL=http://192.65.2.95/BackO
  16. (2, 'Chester')
  17. [InternetShortcut]
  18. URL=http://192.65.3.95/BackO
  19. """
... and so does your code (removed first line since I am using Python25).
Last edited by vegaseat; Oct 25th, 2009 at 2:50 pm.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 25th, 2009
0
Re: Strange outcome using enumerate()
Click to Expand / Collapse  Quote originally posted by vegaseat ...
(removed first line since I am using Python25).
Why don't you install 2.6 ? It's even better.
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,656 posts
since Jul 2008
Oct 25th, 2009
0
Re: Strange outcome using enumerate()
I've just tried it again and it works. How odd, I thought I was going mad!
Reputation Points: 10
Solved Threads: 1
Newbie Poster
dads is offline Offline
5 posts
since Jun 2009
Oct 25th, 2009
0
Re: Strange outcome using enumerate()
Could not reproduce it on python 2.6.2 linux either.
Maybe you have run the pyc version, which was older.
Last edited by slate; Oct 25th, 2009 at 5:16 pm.
Reputation Points: 56
Solved Threads: 65
Posting Whiz in Training
slate is offline Offline
242 posts
since Jun 2008

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: Help with two questions
Next Thread in Python Forum Timeline: Throttle CPU usage





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


Follow us on Twitter


© 2011 DaniWeb® LLC