【问题标题】:Python endswith() reverse?Python endwith() 反转?
【发布时间】:2015-08-27 01:12:41
【问题描述】:

我正在使用 win32com.client 读取 Excel 文件,我想在我拥有的值中找到不以“某些字符串”结尾的所有内容:

ws.Cells(i, 8).Value

由于我不知道 endwith() 的解决方法,我尝试按也可以完成这项工作的值长度进行搜索,但我无法在 ws.Cells(i, 8).Value 上调用 len(),因为我收到“unicode”错误。我还尝试将值转换为字符串,但没有运气。

基本上我想做:

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

感谢我能在这方面获得的任何支持。

【问题讨论】:

  • “我无法在 ws.Cells(i, 8).Value 上调用 len(),因为我收到 'unicode' 错误。”确切的错误是什么?

标签: python excel


【解决方案1】:

只需在 if 语句中使用not

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!"

结果:

this string does not. GOOD!

【讨论】:

  • 谢谢!如果有人想使用 len() 选项,你也可以帮我吗?我真的很好奇如何使用 unicode 值来实现。
  • 我无法复制您的错误。你怎么称呼它?应该是len(ws.Cells(i, 8).Value)
  • 另外,您是否尝试使用.Text而不是.Value?这可能对你有用。 stackoverflow.com/questions/29767898/…
  • 先生,这是我的错!对不起!我也有空单元格,所以我需要ws.Cells(i, 8).Value is not None 的额外条件;再次感谢您。
猜你喜欢
  • 2016-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-20
  • 2015-09-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多