简体   繁体   中英

Python count occurences of a string and print the lines that contain them, as well as print number of occurences of string, with multiple clauses

I've been trying to create a python script that takes in two inputs, a file name and a string. Once it takes those in, it is supposed to print out the number of occurrences of the input string, as well as each line that contains the input string.

I am also required to not use lists, the split method, python string methods, the keyword "in", and i may only use indexing to access the first character of a string, and slicing to get the tail of the string.

What I've done so far:

def main():
  search_for = raw_input("What term would you like to search for?")
  text_file_name = raw_input("Which file would you like to search in?")
  count_text_file(search_for, text_file_name)




def count_text_file(search_for, text_file_name):
   usersFile = open(text_file_name, 'r')
   usersTermLength = len(search_for)
   usersFileLength = len(text_file_name)

   occurenceOfString = 0

    while i<usersFileLength:
        firstChar = usersFile[i]
        if firstChar==searchFor[0]:
            indexUsersTermLength = usersTermLength + i #end slice
            possibleMatch = usersFile[i:indexUsersTermLength]
            if possibleMatch == searchFor:
                print #the entire line
                occurenceOfString+=1
                i+=1
            else: 
                i+=1
        else:
            i+=1

A few issues in your code.

usersFileLength = len(text_file_name)

That's just the length of the name of the file. Not the size of the file's contents.

firstChar = usersFile[i]

This is not how you read from a file. You need to use a function likeread() .

Also, you break a couple of the (silly) constraints. Here's my solution. It reads the entire file then goes through it char-by char. It builds the current word and when it reaches a non-letter it compares.

def count_text_file(search_for, text_file_name):
    with open(text_file_name, 'r') as users_file:
        # Read entire file
        content = users_file.read()
        line_number = 1
        # Build the words of the file char-by-char
        current_word = ""
        while len(content) > 0:
            # "only use indexing to access the first character of a string"
            c = content[0]
            # If it's a letter add to string
            # Can't use c.isalpha() as it is a "python string method"
            if (c >= 'A' and c <= 'Z') or (c >= 'a' and c <= 'z'):
                current_word += c
            # Else (not a letter), check the word
            else:
                if current_word == search_for:
                    print(f"found at line {line_number}")
                if c == '\n':
                    line_number += 1
                # Reset for next word
                current_word = ""
            # "only use ... slicing to get the tail of the string"
            content = content[1:]

There are some improvements you can do. For example, words with punctuation in them won't be found (ex: "can't" or "non-existent"). Also, it only considers a "letter" to be "[A-Za-z]". Unicode characters won't be recognized. And it is case sensitive. But since this is an assignment, who knows if your teacher cares about any of that.

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