【问题标题】:Modify python string with contain method [duplicate]使用包含方法修改python字符串[重复]
【发布时间】:2014-06-09 02:05:45
【问题描述】:

我希望它在 mylist 中找到以下文件:“Microsoft Word 105Prt”(此文件名可能不同,但始终包含“Word”。

for myfile in filelist:
    if myfile.contains("Word"): 
        print myfile

我如何修改它以在 python 2.7.5 中工作,因为 contains 不起作用。

【问题讨论】:

    标签: python-2.7


    【解决方案1】:

    你可以用 find 代替 contains,只检查返回码不是 -1。

    for myfile in filelist:
        if myfile.find("Word")!=-1: 
            print myfile
    

    【讨论】:

      【解决方案2】:

      您可以简单地使用in 关键字,如下所示:

      if 'Word' in myfile:
          print myfile
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-13
        • 1970-01-01
        • 2016-11-20
        相关资源
        最近更新 更多