Python, Unicode, Excel and Web Tesing

C

calfdog

Hello,

I have been doing some experimenting with Automating Internet Explorer
for testing.

I would like to read from Excel and use the data to set the values of
my text boxes on my web forms.
I have been running into problems with Unicode as I can see others
have too.
I have seemed to find a simple solution when printing

Since Excel seems to give back this format (u'Test1',)

If I use this code I get
(u'Test1',)
(u'Test2',)
(u'Test3',)
(u'Test4',)
(u'Test5',)


##########################################
import win32com.client.dynamic

x = win32com.client.dynamic.Dispatch("Excel.Application")
x.Visible=0
x.Workbooks.Open("C:\\Python23\\Lib\\site-packages\\test.xls")
val = x.Range("A2:A20").Value
for i in val:
print i #Prints the unicode charaters

##########################################



If I use the code below it works fine.
Test1
Test2
Test3
Test4
Test5

##########################################
import win32com.client.dynamic

x = win32com.client.dynamic.Dispatch("Excel.Application")
x.Visible=0
x.Workbooks.Open("C:\\Python23\\Lib\\site-packages\\test.xls")
val = x.Range("A2:A20").Value
for i in val:
print i [-1] #Prints without the unicode charaters

##########################################

Does anyone esle ot there have any experience in Automating IE in
Python and using Excel Data?
I am new Python programming but I have managed to write several
functions for Automation I.E.
for setting text values, setting listboxes, links, Clicking buttons
ect. that are working fine.

I am working on porting another tool SAM (written in Perl)
http://samie.sourceforge.net to Python

I would like to share the info on this with other who might have
Automated Internet Explorer with Python.

Thanks
R.
 
P

Peter Otten

(e-mail address removed) wrote:

[problems with excel automation, reportedly due to unicode]

(u'Test1',)

is a tuple, you are doing

(u'Test1',)[-1]

to extract the last item in that tuple, which is, by the way, the same as
the first item (u'Test1',)[0] in this case.

So you may still run into trouble when you inadvertently try to convert your
unicode string into a str *and* it contains characters with ord(c) >= 128.

However, rest assured that Python will spit out a helpful traceback :)

Peter
 
J

John J. Lee

I have been running into problems with Unicode as I can see others
have too.
I have seemed to find a simple solution when printing

Since Excel seems to give back this format (u'Test1',)

If I use this code I get
(u'Test1',)
(u'Test2',)
(u'Test3',)
(u'Test4',)
(u'Test5',) [...]
for i in val:
print i #Prints the unicode charaters

These are length-1 tuples containing unicode strings.


[...]
If I use the code below it works fine.
Test1
Test2
Test3
Test4
Test5 [...]
for i in val:
print i [-1] #Prints without the unicode charaters

These are unicode strings. Why no u'' business? First, you need to
know that str()'s job is to print readable strings. repr(obj)'s job
is to print a string such that when you eval() it, you get back the
same thing you started out with:

ie. repr(u'foo') == "u'foo'"
whereas str(u'foo') == "foo"

Now, print obj prints str(obj), but str(sequence) typically returns a
string containing the repr() of every item in the sequence, NOT the
str() of every item in the sequence. Hence:


John
 
C

calfdog

Peter Otten said:
(e-mail address removed) wrote:

[problems with excel automation, reportedly due to unicode]

(u'Test1',)

is a tuple, you are doing

(u'Test1',)[-1]

to extract the last item in that tuple, which is, by the way, the same as
the first item (u'Test1',)[0] in this case.

So you may still run into trouble when you inadvertently try to convert your
unicode string into a str *and* it contains characters with ord(c) >= 128.

However, rest assured that Python will spit out a helpful traceback :)

Peter

Thanks Peter for the feedback!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,462
Latest member
ChanaLipsc

Latest Threads

Top