简体   繁体   English

Python在txt文件中搜索输入

[英]Python search for input in txt file

When my script is run, it asks for an input.当我的脚本运行时,它要求输入。 That input is then checked to see if it's in a text file.然后检查该输入以查看它是否在文本文件中。 If it is, text is printed.如果是,则打印文本。 The code I have below is what I have but it doesn't seem to be working, any help would be greatly appreciated!我下面的代码是我所拥有的,但它似乎不起作用,任何帮助将不胜感激!

discordname = input("What's your discord name?: ")
file = open('rtf.txt')
for line in file:
    line.strip().split('/n')
    if line.startswith(discordname):

        file.close()
        print("works")

The expression表达方式

line.strip().split('\n')

is not mutating the information bound to the name line , which remains unchanged.不会改变绑定到 name line的信息,它保持不变。 Instead it returns a new value.相反,它返回一个新值。 You need to bind that new value to a name in order to use use it.您需要将该新值绑定到一个名称才能使用它。 This example might help:这个例子可能有帮助:

In [1]: a = "  v  "

In [2]: a.strip()
Out[2]: 'v'

In [3]: a
Out[3]: '  v  '

In [4]: b = a.strip()

In [5]: a
Out[5]: '  v  '

In [6]: b
Out[6]: 'v'

Then split('\\n') (note that you probably want \\ instead of / ) further returns a list of substrings split by newlines.然后split('\\n') (请注意,您可能想要\\而不是/ )进一步返回由换行符拆分的子字符串列表。 Note that this is not very useful because for line in file: already splits over lines so the list would have one element at most, and so you should omit it.请注意,这不是很有用,因为for line in file:已经拆分成行,因此列表最多只有一个元素,因此您应该省略它。

Works for me, Optimized code is:对我有用,优化的代码是:

result =  any(line.startswith(discordname) for line in file.splitlines())
if(result):
    file.close()
    print "works"

You are trying to make the code more complex.您正在尝试使代码更复杂。 Firstly to open a text file use 'with', because it is more flexible and doesn't need closing.首先使用'with'打开一个文本文件,因为它更灵活,不需要关闭。 Then, instead of using strip, you can use readlines().然后,您可以使用 readlines() 而不是使用 strip。 This function converts each line into a list or you can also use method read(), which displays all results as it is.此函数将每一行转换为一个列表,或者您也可以使用方法 read(),它按原样显示所有结果。

So,所以,

By using readlines, you can look through the user input in a line as a list, and by using read, you can look through each words.通过使用 readlines,您可以将一行中的用户输入作为列表进行查看,而通过使用 read,您可以查看每个单词。 Here is the Solution:这是解决方案:

discordname = input("What's your discord name? ")
with open('rtf.txt') as file:
       contents = file.readlines()
if discordname in contents:
       print("It exits")

Here is the Solution:这是解决方案:

discordname = input("What's your discord name?: ")
with open('rtf.txt', 'r') as f:
    for line in f.readlines():
        if line.startswith(discordname):
            print ("it works")

I hope it resolve you're problem.thanks我希望它能解决你的问题。谢谢

You are probably trying to get strings as input as well.您可能也在尝试获取字符串作为输入。 I suggest this:我建议这样做:

discordname = raw_input("What's your discord name? ")
with open('rtf.txt') as f:
    for line in f:
        if discordname in line:
            print "works"

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

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