User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 391,572 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,829 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:
Views: 51420 | Replies: 144
Reply
Join Date: Oct 2006
Posts: 1,159
Reputation: sneekula is on a distinguished road 
Rep Power: 4
Solved Threads: 37
sneekula's Avatar
sneekula sneekula is offline Offline
Veteran Poster

Re: Starting Python

  #141  
Jul 16th, 2008
The module xlrd can read Excel spreadsheets without the use of COM:
  1. # module xlrd is used to extract data from Excel spreadsheets.
  2. # Handles older and newer formats. Can be used on any platform.
  3. # Has support for Excel dates and is unicode-aware.
  4. # download installer xlrd-0.6.1.win32.exe from:
  5. # http://pypi.python.org/pypi/xlrd
  6. # or:
  7. # http://www.lexicon.net/sjmachin/xlrd.htm
  8.  
  9. import xlrd
  10.  
  11. # pick an Excel file you have
  12. book = xlrd.open_workbook("myfile.xls")
  13.  
  14. sheet = book.sheet_by_index(0)
  15.  
  16. for row in range(sheet.nrows):
  17. print sheet.row(row)
Last edited by sneekula : Jul 16th, 2008 at 4:39 pm.
No one died when Clinton lied.
Reply With Quote  
Join Date: Aug 2008
Posts: 1
Reputation: shrikant.telkar is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
shrikant.telkar shrikant.telkar is offline Offline
Newbie Poster

Re: Starting Python

  #142  
19 Days Ago
very nice information
Thank u....
Reply With Quote  
Join Date: Jul 2008
Posts: 1
Reputation: Aneftaoratos is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Aneftaoratos Aneftaoratos is offline Offline
Newbie Poster

Re: Starting Python

  #143  
19 Days Ago
Thanks for the post!
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,394
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 172
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Re: Starting Python

  #144  
5 Days Ago
Sorting lists in Python is simple, but when the list elements are numeric strings like they are in Tkinter or wxPython ListBoxes you need to sort them as floats not strings to make it work properly. Here is an example ...
  1. def cmp_float(s1, s2):
  2. """
  3. s1 and s2 are numeric strings in a list
  4. compare to sort as floats low to high
  5. """
  6. if float(s1) > float(s2): return 1
  7. elif float(s2) > float(s1): return -1
  8. else: return 0
  9.  
  10.  
  11. q = ['4.5', '11.3', '1.6', '9.4', '10.2', '-7.3']
  12.  
  13. print q
  14. print sorted(q)
  15. # you need to supply a custom compare function
  16. print sorted(q, cmp=cmp_float)
  17.  
  18. """
  19. my output -->
  20. ['4.5', '11.3', '1.6', '9.4', '10.2', '-7.3'] <-- unsorted
  21. ['-7.3', '1.6', '10.2', '11.3', '4.5', '9.4'] <-- sorted as strings
  22. ['-7.3', '1.6', '4.5', '9.4', '10.2', '11.3'] <-- sorted as floats
  23. """
May 'the Google' be with you!
Reply With Quote  
Join Date: Jul 2005
Location: France
Posts: 965
Reputation: bumsfeld is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 42
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Posting Shark

Re: Starting Python

  #145  
5 Days Ago
I was working with patient information and came up with this list of instances that made it relatively easy to find specific information:
  1. # creating and processing one list of class objects
  2.  
  3. import operator
  4.  
  5. class Patient(object):
  6. """__init__() functions as the class constructor"""
  7. def __init__(self, fname=None, sname=None, id=None):
  8. self.fname = fname
  9. self.sname = sname
  10. self.id = id
  11.  
  12. def __repr__(self):
  13. """
  14. if you print the instance of the class this format will display
  15. """
  16. # use the class dictionary iterator
  17. class_dictiter = self.__dict__.iteritems()
  18. return " ".join(["%s: %s" % (k,v) for k,v in class_dictiter])
  19.  
  20.  
  21. # make list of class Patient instances
  22. # instance --> surname, firstname, id
  23. patientList = []
  24. patientList.append(Patient("Elmer", "Fudd", "1234"))
  25. patientList.append(Patient("Donald", "Duck", "1235"))
  26. patientList.append(Patient("Mighty", "Mouse", "1236"))
  27. patientList.append(Patient("Fargo", "Ferkel", "1237"))
  28.  
  29. print "Show/test the whole patientList via __repr__ overload:"
  30. print patientList
  31.  
  32. print '-'*60
  33.  
  34. print "Just one instance in the list via __repr__ overload:"
  35. print patientList[3]
  36.  
  37. print '-'*60
  38.  
  39. print "Show one particular item:"
  40. print patientList[0].sname
  41.  
  42. print '-'*60
  43.  
  44. print "Sort the patientList in place by sname ..."
  45. patientList.sort(key=operator.attrgetter('sname'))
  46.  
  47. print
  48.  
  49. print "... then show all patients info in one nice table:"
  50. for patient in patientList:
  51. print "sname: %-20s fname= %-20s id=%s" % \
  52. (patient.sname, patient.fname, patient.id)
  53.  
  54.  
  55. """
  56. my output -->
  57.  
  58. Show/test the whole patientList via __repr__ overload:
  59. [sname: Fudd id: 1234 fname: Elmer, sname: Duck id: 1235 fname: Donald,
  60. sname: Mouse id: 1236 fname: Mighty, sname: Ferkel id: 1237 fname: Fargo]
  61. ------------------------------------------------------------
  62. Just one instance in the list via __repr__ overload:
  63. sname: Ferkel id: 1237 fname: Fargo
  64. ------------------------------------------------------------
  65. Show one particular item:
  66. Fudd
  67. ------------------------------------------------------------
  68. Sort the patientList in place by sname ...
  69.  
  70. ... then show all patients info in one nice table:
  71. sname: Duck fname= Donald id=1235
  72. sname: Ferkel fname= Fargo id=1237
  73. sname: Fudd fname= Elmer id=1234
  74. sname: Mouse fname= Mighty id=1236
  75.  
  76. """
Should you find Irony, you can keep her!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Python Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 10:22 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC