【问题标题】:How do I read a file and convert each line into strings and see if any of the strings are in a user input?如何读取文件并将每一行转换为字符串并查看用户输入中是否有任何字符串?
【发布时间】:2017-06-29 05:35:17
【问题描述】:

我想在我的程序中做的是让程序打开一个包含许多不同单词的文件。

接收用户输入并检查文件中的任何单词是否在用户输入中。

文件内redflags.txt

happy
angry 
ball 
jump

每个单词在不同的行。

例如,如果用户输入是"Hey I am a ball",那么它将打印红旗。 如果用户输入是"Hey this is a sphere",那么它将打印 noflag。

Redflags = open("redflags.txt")

data = Redflags.read()
text_post = raw_input("Enter text you wish to analyse")
words = text_post.split() and text_post.lower()

if data in words:
  print("redflag")
else:
 print("noflag")      

【问题讨论】:

  • 对于初学者来说,这个:words = text_post.split() and text_post.lower() 并没有做你认为它正在做的事情。你想要words = text_post.lower().split()

标签: python string file


【解决方案1】:

这应该可以解决问题!集合通常比查找列表比较快得多。在这种情况下,集合可以告诉您交集(重叠的单词)、差异等。我们使用包含单词列表的文件,删除换行符、小写字母,然后我们有了第一个列表。第二个列表是根据用户输入创建的,并按间距拆分。现在我们可以执行我们设置的交集来查看是否存在任何常用词。

# read in the words and create list of words with each word on newline
# replace newline chars and lowercase
words = [word.replace('\n', '').lower() for word in open('filepath_to_word_list.txt', 'r').readlines()]
# collect user input and lowercase and split into list
user_input = raw_input('Please enter your words').lower().split()

# use set intersection to find if common words exist and print redflag
if set(words).intersection(set(user_input)):
    print('redflag')
else:
    print('noflag')

【讨论】:

【解决方案2】:
with open('redflags.txt', 'r') as f:
    # See this post: https://stackoverflow.com/a/20756176/1141389
    file_words = f.read().splitlines()

# get the user's words, lower case them all, then split
user_words = raw_input('Please enter your words').lower().split()

# use sets to find if any user_words are in file_words
if set(file_words).intersection(set(user_words)):
    print('redflag')
else:
    print('noredflag')

【讨论】:

  • 除了使用set之外,您也可以使用列表生成器,但是集合很好,尤其是在以后您不想要任何重复值的情况下。
【解决方案3】:

我建议您使用列表推导

看看这段代码,它可以满足你的需要(我将在下面解释它们):

Redflags = open("redflags.txt")

data = Redflags.readlines()

data = [d.strip() for d in data]

text_post = input("Enter text you wish to analyse:")
text_post = text_post.split()

fin_list = [i for i in data if i in text_post]


if (fin_list):
    print("RedFlag")
else:
    print("NoFlag")

输出 1:

Enter text you wish to analyse:i am sad
NoFlag

输出 2:

Enter text you wish to analyse:i am angry
RedFlag

所以首先打开文件并使用readlines() 读取它们,这会给出文件中的行列表

>>> data = Redflags.readlines()
['happy\n', 'angry \n', 'ball \n', 'jump\n']

查看所有不需要的空格,换行符 (\n) 使用 strip() 删除它们!但是你不能strip() 一个列表。所以从列表中取出单个项目,然后申请strip()。这可以使用列表推导有效地完成。

data = [d.strip() for d in data]

还有你为什么要使用raw_input()在Python3中使用input()

获取并拆分input 文本后。

fin_list = [i for i in data if i in text_post]

我正在创建一个项目列表,其中data 列表中的i(每个项目)在text_pos 列表中。所以这样我得到了两个列表中的常见项目。

>>> fin_list
['angry']           #if my input is "i am angry"

注意:在python中空列表被认为是false

>>> bool([])
False

虽然那些有价值观的人被认为是

>>> bool(['some','values'])
True

这样你的 if 只在 list 非空时执行。 含义 'RedFlag' 仅当在两个列表之间找到一些common 项时才会打印。你得到你想要的。

【讨论】:

    猜你喜欢
    • 2015-10-03
    • 1970-01-01
    • 2011-06-26
    • 2012-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-11
    • 1970-01-01
    相关资源
    最近更新 更多