Search Multiple Strings (from File) in a file and print the line

后端 未结 3 1412
無奈伤痛
無奈伤痛 2021-01-23 22:13

Again apologies for been noob here: Trying below code for searching multiple strings read from keywords and search in f and printing the line. It works if I have on

3条回答
  •  终归单人心
    2021-01-23 22:42

    keywords = input("Please Enter keywords path as c:/example/ \n :")
    keys = open((keywords), "r").readline()
    keys = keys.split(',')  # separates key strings
    with open("c:/saad/saad.txt") as f:
        for line in f:
            for key in keys:
                if key.strip() in line:
                    print(line)
    

    You are reading the line in as one string. You need to make a list of each comma separated string. Then test each key for each line (removing whitespace around the key)

    This is assuming your keyword file is something like: aa is good, bb is good, spam, eggs

提交回复
热议问题