简体   繁体   English

Python:在文本文件上搜索字符串并显示匹配的行

[英]Python: Search for string on a text file and display matching rows

I have this program right now where it allows users to choose from a category(pulling from the file).我现在有这个程序,它允许用户从一个类别中进行选择(从文件中提取)。 Then it will print the University or people from that text file.然后它将打印该文本文件中的大学或人。

What I want to do next on my code is for users to search for a specific string from that file and it will display both the University and People that have that matching string.我接下来要在我的代码中做的是让用户从该文件中搜索特定字符串,它将显示具有该匹配字符串的大学和人员。 It can be the whole word or part of the string from that file.它可以是该文件中的整个单词或字符串的一部分。

I need help on searching for a given string or part of a string and display matching categories (University and People).我需要有关搜索给定字符串或字符串的一部分并显示匹配类别(大学和人物)的帮助。

Example:例子:

search: ohn搜索: ohn

output:输出:

University: UCLA Name: J ohn Kelly大学:加州大学洛杉矶分校姓名:约翰凯利

University: U of Memphis Name: Taylor J ohn son大学:孟菲斯大学 姓名:Taylor J ohn son

Here is my current text file:这是我当前的文本文件:

"UniversityName","ContactName"
"UCLA","John Kelly"
"UofFlorida","Mary Elizabeth"
"U of Memphis","Taylor Johnson"
"Harvard","Robert Fax"

This is what I've done so far with my code:这是我到目前为止对我的代码所做的:

def load_data(file_name):
    university_data=[]
    with open(file_name,'r') as rd_file:
        for line in rd_file.readlines():
            line=line.strip().split(',')
            T = line[0],line[1]
            university_data.append(T)
    return university_data

def main():
    filename='List.txt'
    university_data = load_data(filename)
    print('[1] University\n[2] Contact Name\n[3] Exit\n[4] Search')
    while True:

        choice=input('Enter choice 1/2/3? ')
        if choice=='1':
            for university in university_data:
                print(university[0])
        elif choice=='2':
            for university in university_data:
                print(university[1])
        elif choice =='3':
            print('Thank You')
            break
        elif choice =='4':
            print("Search Section Here")
        else:
            print('Invalid selection')

main()

Given your data:鉴于您的数据:

university_data = [["UniversityName","ContactName"],
["UCLA","John Kelly"],
["UofFlorida","Mary Elizabeth"],
["U of Memphis","Taylor Johnson"],
["Harvard","Robert Fax"]]

Use str.join :使用str.join

def search(key, data):
    for l in data:
        if key in ''.join(l):
            print('University: %s Name: %s' % (l[0], l[1]))
search('ohn', university_data)
# University: UCLA Name: John Kelly
# University: U of Memphis Name: Taylor Johnson

Similar to the previous answer by @Chris , in case you want to ignore the upper or lowercase of the search word, you can add lower() in the search function.@Chris的先前回答类似,如果您想忽略搜索词的大写或小写,可以在search功能中添加lower()

def search(key, mydata):
    for l in mydata:
        if key in (''.join(l)).lower():
            print('University: %s Name: %s' % (l[0], l[1])) 

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

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