0

I am trying to automate a task of copying data from excel to another application based on a "tagname" column that exists in both. I'm having Python copy the "tagname" and save it to a variable for each ("metertag" and "exceltag"). I've thrown up a tkMessageBox right before the compare to confirm the values are the same (and they are), but my if statement is not being executed, the else is. My question is: does python treat excel formatted text differently from other copied texts? (How would I fix that?) Otherwise what else could be the problem?

click(573,745)
time.sleep(.3)
click_hold(625,357)
win32api.SetCursorPos((571,357))
click_release(571,357)
CTCopy()
time.sleep(.3) 
click(1116,264)

win32clipboard.OpenClipboard()
metertag = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()

time.sleep(.1)
click(2295, 252) #top of tags excel 
time.sleep(1)
click(2295, 252) #top of tags excel
time.sleep(1)

for x in range (0,499):
CTCopy()
win32clipboard.OpenClipboard()
exceltag = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
time.sleep(.02)

metertag = str(metertag)    
exceltag = str(exceltag)
TS(metertag,exceltag)

if metertag == exceltag:   #<-- this is not true even when I know it is!!?
    time.sleep(.25)
    press('left_arrow')
    time.sleep(.25)
    press('left_arrow')
    CTCopy()
    click(1005,747) #"enter reading" button for updating reading
    time.sleep(.75)
    click(771,580)  #text box for reading update
    time.sleep(.05)
    CTPaste()
    time.sleep(.05)
    time.sleep(.05)
    click(913,580)  #"update now" button
    #need case when pop-up occurs
    break
else:
    press('down_arrow')
Beutler
  • 83
  • 1
  • 8
  • Maybe because the encodings are different? Excel is not utf-8? Check http://stackoverflow.com/a/1502134/1860929 and http://stackoverflow.com/q/5806980/1860929 – Anshul Goyal Sep 10 '14 at 14:49
  • Thanks for your answer Mu. That's my thought, I tested them as ascii characters and didn't get back any errors. How can I ensure they are encoded the same? I tried str() as above, I've also tried: metertag = metertag.decode("ascii") exceltag = exceltag.decode("ascii") – Beutler Sep 10 '14 at 15:56

1 Answers1

1

I have the answer!! For whatever reason, copying from excel using the clipboard as above also adds a "\n" - I finally looked at the length of each - one was 8, the other 10. I then did a concatonation with a dummy letter for each to discover it was putting in a new line!

Beutler
  • 83
  • 1
  • 8
  • Two things: first, print `repr(str)` when debugging to see the hidden characters. Second, when comparing values that aren't supposed to have whitespace around them you can use `.strip()` to remove it all, including `\r` and `\n`. – Mark Ransom Sep 10 '14 at 18:43
  • @Mark Ransom Thanks Mark! That is a more elegant way to test =p Still getting my feet wet in python! – Beutler Sep 11 '14 at 12:00