468,288 Members | 1,989 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,288 developers. It's quick & easy.

convert COM obj to integer

I'm pulling a range of cells from Excel into a list and the data in
Excel is a number or possibly some text like an asterisk. Each member
of the list is a com object (I think) and I'm converting them to
integers (or to None if not numberic) but my method seems very silly.
Is this the best way to go about it?

It does exactly what it should, but it seems like a lot of extra BS to
convert my object to a string to a float to a rounded integer! Don't
laugh. I'm new at this!
THE SCRIPT:----------------------------------

import win32com.client
xlApp = win32com.client.Dispatch("Excel.Application")
f = r"C:\py\TrafficVolumes\xlTestDocs\3125113A.xls"
xlApp.Visible = 0
xlApp.Workbooks.Open(f)
list = xlApp.ActiveWorkbook.ActiveSheet.Range("Q13:Q36")

print list
print "\n"
def comObjToInteger(myObj):
try:
s = str(myObj)
fl = float(s)
integer = int(round(fl))
return integer
except:
return None
for i in list:
print comObjToInteger(i)
xlApp.ActiveWorkbook.Close(SaveChanges=0)
xlApp.Quit()

del xlApp #clean up
----------------------------------------------

THE RESULT:--------------------
the list:

((4.7999999999999998,), (u'*',), (2.0,), (1.6000000000000001,),
(5.5999999999999996,), (19.399999999999999,), (25.0,),
(38.799999999999997,), (32.799999999999997,), (21.0,), (24.0,),
(17.399999999999999,), (22.800000000000001,), (22.600000000000001,),
(33.799999999999997,), (35.399999999999999,), (29.199999999999999,),
(35.399999999999999,), (32.200000000000003,), (26.0,),
(24.399999999999999,), (22.800000000000001,), (14.0,), (11.6,))

my converted values:
5
None
2
2
6
19
25
39
33
21
24
17
23
23
34
35
29
35
32
26
24
23
14
12
----------------------------------

Nov 2 '05 #1
1 2364
I don't know exactly what a COM object is, but those aren't them. The
win32com package takes care of converting everything to Python types.
The excel call returns a tuple of tuples. That is, the outer tuple is
the sequence of rows, and each such row is itself a tuple with one
member per column requested. Since you only request one column, it is a
one-item-long tuple, also called a 1-tuple. That is demonstrated by the
result of print'ing the list.

By the way, you shouldn't use 'list' as a name because it is also the
name of a built-in function. And it isn't a list anyway, it's a tuple.

Now, each number is in fact already a primitive Python object of type
float. (The asterisk is a unicode string.) So you want to convert the
floats into integers, and it looks like you want to round rather than
truncate.
----
table = xlApp.ActiveWorkbook.ActiveSheet.Range("Q13:Q36")

converted_values = []

for row in table:
value = row[0] #get the first (and only) item in the tuple
try:
value = round(value)
except TypeError: #value is not a float
value = None
else:
value = int(value) #turn the float into an int
converted_values.append(value)
print converted_values
----

By the way, if you wonder how I knew to catch the TypeError, I just
fired up the interactive Python interpreter, and typed this: round(u'*')

Nov 2 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Logan X via .NET 247 | last post: by
3 posts views Thread by Convert TextBox.Text to Int32 Problem | last post: by
3 posts views Thread by Mike Miller | last post: by
7 posts views Thread by Scott Schluer | last post: by
5 posts views Thread by Mika M | last post: by
20 posts views Thread by Niyazi | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.