0

I am using the win32com.client to read an Excel file and I would like to find everything NOT ending with "some string" in the value(s) I have in:

ws.Cells(i, 8).Value

Since I do not know the workaround for endswith(), I tried to also search by the value length which could also do the job, but I cannot call len() on ws.Cells(i, 8).Value because I get an 'unicode' error. I also tried to convert the value to a string without luck.

Basically I would like to do:

if len(ws.Cells(i, 8).Value) > 255:
    ws.Cells(i, 8).Value = ws.Cells(i, 8).Value + " (Issues Here)"

I appreciate any support I can get on this.

Robert
  • 521
  • 2
  • 8
  • 14
  • "I cannot call len() on ws.Cells(i, 8).Value because I get an 'unicode' error." What is the exact error? – Kevin Aug 26 '15 at 17:07

1 Answers1

1

Just use not in the if statement.

string_list = ["this ends in some string", "this string does not"]

for string in string_list:
    if not string.endswith('some string'):
        print string, ". GOOD!"

result:

this string does not. GOOD!
f.rodrigues
  • 3,499
  • 6
  • 26
  • 62
  • Thank you! Can you also help me with the len() option, if one would want to use it? I am really curious how that could be accomplished with a unicode value. – Robert Aug 26 '15 at 17:25
  • I can't replicate your error. How are you calling it? it should be `len(ws.Cells(i, 8).Value)` – f.rodrigues Aug 26 '15 at 17:35
  • Also, did you try using `.Text`instead of `.Value`? This might be useful to you. http://stackoverflow.com/questions/29767898/how-to-read-excel-unicode-characters-using-python – f.rodrigues Aug 26 '15 at 17:38
  • 1
    Sir, it was my bad! Sorry! I also had empty cells so I needed the extra condition of `ws.Cells(i, 8).Value is not None`; Thank you again. – Robert Aug 26 '15 at 17:59