•
•
•
•
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
![]() |
The module xlrd can read Excel spreadsheets without the use of COM:
python Syntax (Toggle Plain Text)
# module xlrd is used to extract data from Excel spreadsheets. # Handles older and newer formats. Can be used on any platform. # Has support for Excel dates and is unicode-aware. # download installer xlrd-0.6.1.win32.exe from: # http://pypi.python.org/pypi/xlrd # or: # http://www.lexicon.net/sjmachin/xlrd.htm import xlrd # pick an Excel file you have book = xlrd.open_workbook("myfile.xls") sheet = book.sheet_by_index(0) for row in range(sheet.nrows): print sheet.row(row)
Last edited by sneekula : Jul 16th, 2008 at 4:39 pm.
No one died when Clinton lied.
•
•
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,394
Reputation:
Rep Power: 9
Solved Threads: 172
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 ...
python Syntax (Toggle Plain Text)
def cmp_float(s1, s2): """ s1 and s2 are numeric strings in a list compare to sort as floats low to high """ if float(s1) > float(s2): return 1 elif float(s2) > float(s1): return -1 else: return 0 q = ['4.5', '11.3', '1.6', '9.4', '10.2', '-7.3'] print q print sorted(q) # you need to supply a custom compare function print sorted(q, cmp=cmp_float) """ my output --> ['4.5', '11.3', '1.6', '9.4', '10.2', '-7.3'] <-- unsorted ['-7.3', '1.6', '10.2', '11.3', '4.5', '9.4'] <-- sorted as strings ['-7.3', '1.6', '4.5', '9.4', '10.2', '11.3'] <-- sorted as floats """
May 'the Google' be with you!
I was working with patient information and came up with this list of instances that made it relatively easy to find specific information:
python Syntax (Toggle Plain Text)
# creating and processing one list of class objects import operator class Patient(object): """__init__() functions as the class constructor""" def __init__(self, fname=None, sname=None, id=None): self.fname = fname self.sname = sname self.id = id def __repr__(self): """ if you print the instance of the class this format will display """ # use the class dictionary iterator class_dictiter = self.__dict__.iteritems() return " ".join(["%s: %s" % (k,v) for k,v in class_dictiter]) # make list of class Patient instances # instance --> surname, firstname, id patientList = [] patientList.append(Patient("Elmer", "Fudd", "1234")) patientList.append(Patient("Donald", "Duck", "1235")) patientList.append(Patient("Mighty", "Mouse", "1236")) patientList.append(Patient("Fargo", "Ferkel", "1237")) print "Show/test the whole patientList via __repr__ overload:" print patientList print '-'*60 print "Just one instance in the list via __repr__ overload:" print patientList[3] print '-'*60 print "Show one particular item:" print patientList[0].sname print '-'*60 print "Sort the patientList in place by sname ..." patientList.sort(key=operator.attrgetter('sname')) print "... then show all patients info in one nice table:" for patient in patientList: print "sname: %-20s fname= %-20s id=%s" % \ (patient.sname, patient.fname, patient.id) """ my output --> Show/test the whole patientList via __repr__ overload: [sname: Fudd id: 1234 fname: Elmer, sname: Duck id: 1235 fname: Donald, sname: Mouse id: 1236 fname: Mighty, sname: Ferkel id: 1237 fname: Fargo] ------------------------------------------------------------ Just one instance in the list via __repr__ overload: sname: Ferkel id: 1237 fname: Fargo ------------------------------------------------------------ Show one particular item: Fudd ------------------------------------------------------------ Sort the patientList in place by sname ... ... then show all patients info in one nice table: sname: Duck fname= Donald id=1235 sname: Ferkel fname= Fargo id=1237 sname: Fudd fname= Elmer id=1234 sname: Mouse fname= Mighty id=1236 """
Should you find Irony, you can keep her!
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
- Clear the console screen (Python)
- Re: Starting Python (Python)
Other Threads in the Python Forum
- Previous Thread: Wizards Warlocks & Window.Refresh()
- Next Thread: Pygame Cursor



Linear Mode