简体   繁体   中英

How to search a text file for a specific word in Python

I want to find words in a text file that match words stored in an existing list called items, the list is created in a previous function and I want to be able to use the list in the next function as well but I'm unsure how to do that, I tried using classes for that but i couldn't get it right. And I can't figure out what the problem is with the rest of the code. I tried running it without the class and list and replaced the list 'items[]' in line 8 with a word in the text file being opened and it still didn't do anything, even though no errors come up. When the below code is run it prints out: "Please entre a valid textfile name: " and it stops there.

class searchtext():
    textfile = input("Please entre a valid textfile name: ")
    items = []

    def __init__search(self):
        with open("textfile") as openfile:
            for line in openfile:
                for part in line.split():
                    if ("items[]=") in part:
                        print (part)
                    else:
                        print("not found") 

The list is created from another text file containing words in a previous function that looks like this and it works as it should, if it is to any help:

def createlist():
    items = []
    with open('words.txt') as input:
        for line in input:
            items.extend(line.strip().split(','))
    return items

print(createlist())

You can use regexp the following way:

    >>> import re
    >>> words=['car','red','woman','day','boston']
    >>> word_exp='|'.join(words)
    >>> re.findall(word_exp,'the red car driven by the woman',re.M)
    ['red', 'car', 'woman']

The second command creates a list of acceptable words separated by "|". To run this on a file, just replace the string in 'the red car driven by the woman' for open(your_file,'r').read() .

This may be a bit cleaner. I feel class is an overkill here.

def createlist():
    items = []
    with open('words.txt') as input:
        for line in input:
            items.extend(line.strip().split(','))
    return items

print(createlist())
# store the list
word_list = createlist()

with open('file.txt') as f:
    # split the file content to words (first to lines, then each line to it's words)
    for word in (sum([x.split() for x in f.read().split('\n')], [])):
        # check if each word is in the list
        if word in word_list:
            # do something with word
            print word + " is in the list"
        else:
            # word not in list
            print word + " is NOT in the list"

There is nothing like Regular expressions in matching https://docs.python.org/3/howto/regex.html

items=['one','two','three','four','five'] #your items list created previously
import re
file=open('text.txt','r') #load your file
content=file.read() #save the read output so the reading always starts from begining
for i in items:
    lis=re.findall(i,content)
    if len(lis)==0:
        print('Not found')
    elif len(lis)==1:
        print('Found Once')
    elif len(lis)==2:
        print('Found Twice')
    else:
        print('Found',len(lis),'times')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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