473,786 Members | 2,398 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("Exce l.Application")
f = r"C:\py\Traffic Volumes\xlTestD ocs\3125113A.xl s"
xlApp.Visible = 0
xlApp.Workbooks .Open(f)
list = xlApp.ActiveWor kbook.ActiveShe et.Range("Q13:Q 36")

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.ActiveWor kbook.Close(Sav eChanges=0)
xlApp.Quit()

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

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

((4.79999999999 99998,), (u'*',), (2.0,), (1.600000000000 0001,),
(5.599999999999 9996,), (19.39999999999 9999,), (25.0,),
(38.79999999999 9997,), (32.79999999999 9997,), (21.0,), (24.0,),
(17.39999999999 9999,), (22.80000000000 0001,), (22.60000000000 0001,),
(33.79999999999 9997,), (35.39999999999 9999,), (29.19999999999 9999,),
(35.39999999999 9999,), (32.20000000000 0003,), (26.0,),
(24.39999999999 9999,), (22.80000000000 0001,), (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 3113
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.ActiveWor kbook.ActiveShe et.Range("Q13:Q 36")

converted_value s = []

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_value s.append(value)
print converted_value s
----

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1787
by: Logan X via .NET 247 | last post by:
It's official....Convert blows. I ran a number of tests converting a double to an integer usingboth Convert & CType. I *ASSUMED* that CType would piggy-back ontop of Convert, and that performance would be identical. I was 100% incorrect. The code below produces the results: CType Took: 0.2187528 seconds. Convert Took: 12.187656 seconds.
3
10294
by: Convert TextBox.Text to Int32 Problem | last post by:
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function and there you have it. So I enquired and found the Convert class with it's promising ToInt32 method, great... but it doesn't work. The thing keeps throwing Format Exceptions all over the place. What is the "C#" way to do this??? code int wmin,...
3
9332
by: Mike Miller | last post by:
What is the best way to convert a managed unsigned int64 to an unsigned long? Basically I need to do the following: System::UInt64 managedInt = 10; unsigned long unmanagedInt; unmanagedInt = managedInt; Also it would be nice to know how to do this: System::UInt64 managedInt;
4
7283
by: Andreas Klemt | last post by:
Hello, what has the better performance and what are you using? Dim myObj As Object = 70 a) Dim myInt As Integer = DirectCast(myObj, Integer) b) Dim myInt As Integer = Convert.ToInt32(myObj) Thanks, Andreas
7
3400
by: Scott Schluer | last post by:
Is there a way to use the Image class to convert a color photo (GIF or JPEG) to a B&W photo? Thanks, Scott
6
43663
by: MrKrich | last post by:
I want to convert Hexadecimal or normal integer to Binary. Does VB.Net has function to do that? I only found Hex function that convert normal integer to Hexadecimal.
5
37337
by: Mika M | last post by:
Hi! I've made little code to convert string into hex string... Public ReadOnly Property ToHexString(ByVal text As String) As String Get Dim arrBytes As Integer() = CharsToBytes(text) Dim sb As StringBuilder = New StringBuilder For i As Integer = 0 To arrBytes.Length - 1
20
3447
by: Niyazi | last post by:
Hi all, I have a integer number from 1 to 37000. And I want to create a report in excel that shows in 4 alphanumeric length. Example: I can write the cutomerID from 1 to 9999 as: 1 ----> 0001 2 ----> 0002
5
8651
by: Bob Homes | last post by:
In VB6, foreground and background colors of controls had to be assigned a single number. If you knew the RGB values for the color, you still had to convert them into the single number accepatable to the VB6 controls. In VB.NET, you can't set colors that way anymore, now you have to use a "color". There is a way to convert RGB values to a "color", using the Color.FromARGB method. But there doens't seem to be a way to convert the old...
7
6268
by: shellon | last post by:
Hi all: I want to convert the float number to sortable integer, like the function float2rawInt() in java, but I don't know the internal expression of float, appreciate your help!
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9961
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8989
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3669
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.