简体   繁体   English

Python:startswith() 未按预期工作

[英]Python: startswith() not working as intended

I am writing a program that loads an external txt file of movies.我正在编写一个加载外部 txt 电影文件的程序。 This part works fine.这部分工作正常。 I then have a function that searches a list of movies generated from the file.然后我有一个函数来搜索从文件生成的电影列表。 The function should print out all movies that start with the search string.该函数应该打印出所有以搜索字符串开头的电影。

def startsWithSearch(movieList):

    searchString = input("Enter search string: ")

    for movie in movieList:
        if(movie.startswith(searchString) == True):
        print(movie)

However, no movies are printed when I enter a search string, even though there are movies in the list that start with that string.但是,当我输入搜索字符串时,即使列表中有以该字符串开头的电影,也不会打印任何电影。

if given correct input data your function does work as expected:如果给出正确的输入数据,您的函数会按预期工作:

def startsWithSearch(movieList):

    searchString = "test4"

    for movie in movieList:
        if(movie.startswith(searchString)):
            print(movie)


startsWithSearch(["test1","testnomatch","test4","test4should","not_test4"])

output is:输出是:

test4
test4should

so all correct... must be your input data所以一切都正确......必须是你的输入数据

i know you want a StartsWith solution as your function name says, but actually searching for movies, it is a lot more convenient, if you find any match inside the string, so if i search for "mentalist" i will find "the mentalist", then you could just use:我知道你想要一个 StartsWith 解决方案,正如你的函数名称所说,但实际上搜索电影,它更方便,如果你在字符串中找到任何匹配项,所以如果我搜索“mentalist”,我会找到“the mindist” ,那么你可以使用:

if searchString in movie:
    print(movie)

And as suggested by Anna to ignore case:正如安娜所建议的忽略大小写:

if searchString.lower() in movie.lower():

or even fancier with regular expressions (need import re at first line):甚至更喜欢正则表达式(需要在第一行import re ):

if re.match(".*" + searchString,movie,re.I):

or if you really just want match on beginning of name:或者,如果您真的只想匹配名称开头:

if re.match(searchString,movie,re.I):

that should be enough alternatives :)那应该是足够的替代品:)

I would think it might be that the input() function is returning with a new line at the end.我认为可能是input()函数在末尾返回了一个新行。 Try adding searchString = searchString.strip() after collecting the input data.收集输入数据后尝试添加searchString = searchString.strip() Also you might want to try converting both to lower case before comparing.此外,您可能想在比较之前尝试将两者都转换为小写。

Also the line if(movie.startswith(searchString) == True): can just be written as if movie.startswith(searchString):此外, if(movie.startswith(searchString) == True):也可以像if movie.startswith(searchString):那样写if movie.startswith(searchString):

I had same problem when I was reading from files.当我从文件中读取时,我遇到了同样的问题。 startswith was failing due to newline character which was read from file.由于从文件中读取换行符, startswith失败。 Use rstrip() to remove newline character from both strings before you use startswith function.在使用startswith函数之前,使用rstrip()从两个字符串中删除换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM